-1

I have this code right here.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;

NSData* imgData=UIImagePNGRepresentation(chosenImage);
[[NSUserDefaults  standardUserDefaults] setObject:imgData forKey:@"image"];
[[NSUserDefaults  standardUserDefaults]synchronize];

[picker dismissViewControllerAnimated:YES completion:NULL];

}

and then in another view I have

-(void)showImage{
NSData* image=[[NSUserDefaults  standardUserDefaults]        objectForKey:@"image"];
UIImage* img=[UIImage imageWithData:image];
//now you can use that img image on any imageview
UIImageView *imageView = [[UIImageView alloc] initWithImage:img];
}

I'm trying nsuserdefaults to load the image in the second view, but no matter what I do it won't show in the UIImageView.

Please don't tell me to not use nsuserdefaults I have to use it

  • 1
    Do you have an image view? Have you looked at the docs for `UIImageView`? It has a pretty obvious property for setting the image. – rmaddy May 12 '14 at 22:30
  • 2
    You really shouldn't be storing image data in NSUserDefaults. – Logan May 12 '14 at 22:31
  • BTW - your statement that you have to use `NSUserDefaults` doesn't make sense. Why? Whoever told you that doesn't understand proper iOS development. – rmaddy May 12 '14 at 23:27
  • In your updated code, you are not doing anything with the new image view you create. If you already have an image view outlet, use it instead of creating a new one. – rmaddy May 12 '14 at 23:28

1 Answers1

0

UIImageView *imageView = [[UIImageView alloc] initWithImage:img]

And obligatory, don't store images in NSUserDefaults

Chris Wagner
  • 20,773
  • 8
  • 74
  • 95