我有一个UIButton
用户点击它以显示UIImagePickerController
。处理完成后,它会将已编辑的UIImage
返回给委托处理程序,然后该处理程序将使用新映像填充UIButton
。
然而,实际上,如果用户从他们的库中选择一个图像,它会正常工作。但如果他们使用相机拍摄照片并对其进行编辑,则图像无法显示UIButton
。但是,如果我将相同的图像放入UIImageView
进行测试,则会显示出来。
此外,这在模拟器中工作正常,但在设备上不起作用。这是某种记忆问题吗?这是我的代码:
- (IBAction)takePictureButtonTapped
{
UIActionSheet *popupQuery = [[UIActionSheet alloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Take a Photo", @"Upload from Library", nil];
popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[popupQuery showInView:self.view];
[popupQuery release];
}
- (void)actionSheet:(UIActionSheet *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"actionSheet:clickedButtonAtIndex:%d", buttonIndex);
if(buttonIndex == 2)
{
// cancel
[imagePickerController release];
}
else
{
imagePickerController = [[[UIImagePickerController alloc] init] autorelease];
imagePickerController.delegate = self;
imagePickerController.allowsEditing = YES;
if (buttonIndex == 0)
{
// Take a photo
// Set up the image picker controller and add it to the view
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
}
else if (buttonIndex == 1)
{
// Upload from Library
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary] == NO)
imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
[self presentModalViewController:imagePickerController animated:YES];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)img
editingInfo:(NSDictionary *)editInfo
{
NSLog(@"imagePickerController::didFinishPickingImage:%@", img);
itemImage = img;
[itemImage retain];
[imageButton setBackgroundImage:itemImage forState:UIControlStateNormal];
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
我为所有州尝试了setImage
,setBackgroundImage
,但没有一个能够奏效。然而,如果我将相同的图像放入UIImageView
,那很好。
有什么想法吗?
答案 0 :(得分:2)
我发现它无法正常工作的原因是因为我使用的是didFinishPickingImage:editingInfo:
已弃用。我应该一直在使用imagePickerController:didFinishPickingMediaWithInfo:
。现在它完美运作了!
出于某种原因,XCode没有警告我弃用。