1

I am trying to update an image on the Viewcontroller, I am using ImagePickerController and I could able to see chosenImage has data, and I assign it. When image is chosen and then this viewcontroller is loading again, I could able to debug to see whether or not it hits loadUserProfile method, yes it is. But UIImage is getting nil in somewhere and somehow.

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

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

-(void)loadUserProfile
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString * userImageURL = [defaults objectForKey:@"imageURL"];    
    bool isReload = [defaults boolForKey:@"isReload"];

    if(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];
        }
    }];
}
Antony Raphel
  • 2,036
  • 2
  • 25
  • 45
casillas
  • 16,351
  • 19
  • 115
  • 215
  • Hm, did you check the `self` in `didFinishPickingMediaWithInfo` and `loadUserProfile` is the same address or not? – nynohu Feb 27 '17 at 06:29
  • check with this post and do the modification of the code based on your requirement. http://www.theappguruz.com/blog/ios-image-picker-controller – Antony Raphel Feb 27 '17 at 06:32

2 Answers2

1
  • 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.
Community
  • 1
  • 1
Gokul G
  • 2,046
  • 15
  • 22
1

try this code because when you load again vc than image data is nil so first convert image data to base64string .

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

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

-(void)loadUserProfile
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString * userImageURL = [defaults objectForKey:@"imageURL"];
    bool isReload = [defaults boolForKey:@"isReload"];

    if(isReload)
    {
        //self.userPicImage is always nil
        NSString *base64String=[defaults stringForKey:@"chosenimage"];
        NSData* data = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
        UIImage* image = [UIImage imageWithData:data];
        self.userProfileImage.image=image;
      //  [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;
    NSData *imageData = UIImageJPEGRepresentation(chosenImage, 1.0);
    NSString *encodedString = [imageData base64Encoding];

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

    [picker dismissViewControllerAnimated:YES completion:^{
        if (source == UIImagePickerControllerSourceTypeCamera) {
            [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
        }
    }];
}
Jigar
  • 1,801
  • 1
  • 14
  • 29