hotspring hotspring -4 years ago 120
iOS Question

UIImage is getting nil from the UIImagePickerController

I am trying to update an image on the

, I am using
and I could able to see
has data, and I assign it. When image is chosen and then this
is loading again, I could able to debug to see whether or not it hits
method, yes it is. But
is getting nil in somewhere and somehow.

@property (strong, nonatomic) UIImage *userPicImage;
@property (weak, nonatomic) IBOutlet UIImageView *userProfileImage;

- (void)viewDidLoad {
[super viewDidLoad];
[self loadUserProfile];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString * userImageURL = [defaults objectForKey:@"imageURL"];
bool isReload = [defaults boolForKey:@"isReload"];

//self.userPicImage is always nil
[self.userProfileImage setImage:self.userPicImage];
[defaults setBool:false forKey:@"comingBack"];
[defaults synchronize];

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImagePickerControllerSourceType source = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypeSavedPhotosAlbum;

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
// userPicImage is not nil here!
self.userPicImage = chosenImage;

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:true forKey:@"isReload"];
[defaults synchronize];

[picker dismissViewControllerAnimated:YES completion:^{
if (source == UIImagePickerControllerSourceTypeCamera) {
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];

Answer Source
  • Whenever viewdidload is called the viewcontroller is loaded as a new one reference, all of its reference outlets and variables are set to new. In your case userPicImage is initiated as new object so logically there wont be any content in it.
  • You better store your image data in userdefaults and retrieve it from there.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download