I’ve checked several tutorials and from what I can tell my code is correct, but at the end I don’t get the image. I don’t get any error, but the image on the screen I’m trying to set is still blank.
Here’s my header file
@interface homeViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *avatarImage;
@property (weak, nonatomic) IBOutlet UIButton *photoButton;
@property (nonatomic, retain) UIImagePickerController *imgPicker;
- (IBAction)setPhoto;
@end
And here’s the related methods
- (IBAction)setPhoto
{
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.sourceType=UIImagePickerControllerSourceTypeCamera;
self.imgPicker.delegate=self;
self.imgPicker.allowsEditing = YES;
self.imgPicker.showsCameraControls=YES;
[self presentModalViewController:self.imgPicker animated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
self.avatarImage = [[UIImageView alloc] init];
self.avatarImage.image = image;
[self dismissModalViewControllerAnimated:YES];
}
When I place a breakpoint here: [self dismissModalViewControllerAnimated:YES];
I see that the image variable has a value, but the self.avatarImage has a 0x00000000
Since you specified
allowsEditing = NOyou need to use theUIImagePickerControllerOriginalImageinfo key.Note: it may be that ‘EditedImage’ defaults to ‘OriginalImage’ if no editing occurs. In that case your ‘image’ variable will be valid and the display of the UIImageView in self.avatarView will be the culprit.