UIImage doesn't show up from camera, just from photo library
I'm using the following code in my UIImagePickerController
delegate:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = (UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage];
[self.personPhotoButton setBackgroundImage:image forState:UIControlStateNormal];
[self dismissModalViewControllerAnimated:YES];
}
It works perfectly if the image is from the user's photo library, but if it's coming from the camera, no image shows up in the button. There aren't any errors or crashes, it just doesn't work (the button background stays the same). When I use NSLog
on info
, it shows that there is an image object in UIImagePickerControllerOriginalImage
, but it just doesn't get displayed. I've tried resizing it, but that doesn't change anything.
Any thoughts?
EDIT: I'm able to sorta get it working by adding the following to the above method:
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:conte开发者_如何学CxtInfo:), nil);
And then, I have this method:
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
[self.personPhotoButton setBackgroundImage:image forState:UIControlStateNormal];
}
So, does a photo need to be saved in order to be displayed? Can I not display a UIImage unless it's definitely saved to the photo album? Ideally, I'd rather not have to do it this way, but it seems like that's the route I might have to go. I'll also try saving it to disk (not to photo library), maybe that will work.
It turns out the issue was where I was calling dismissModalViewControllerAnimated:
. If I move it to the top of the method, everything works as expected:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissModalViewControllerAnimated:YES];
UIImage *image = (UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage];
[self.personPhotoButton setBackgroundImage:image forState:UIControlStateNormal];
}
Maybe you should retain your image before adding it as background.
精彩评论