1

I am saving image in nsuserdefaulf in my first viewcontroller where i am getting image from imagepicker

[[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(image)            forKey:@"image"];

and i am getting this image in next viewController like this

 NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:@"image"];
 UIImage* image = [UIImage imageWithData:imageData];

but when i am passing this image for use it giving me error but when i am using image like this than it works. ( bg.png is saved inside project)

UIImage *image = [UIImage imageNamed:@"bg.png"];

Whats the problem with my code. ?

Zohaib
  • 2,845
  • 2
  • 23
  • 33
  • Did you synchronise it ?? if not do it [[NSUserDefaults standardUserDefaults] synchronize]; – Manu Mar 19 '13 at 07:09
  • 3
    I suggest you to not do using NSUserDefaults. – Manu Mar 19 '13 at 07:10
  • than what should i use to store an image from imagepicker and use it in another viewcontroller? – Zohaib Mar 19 '13 at 07:11
  • NSUserDefaults is for persisting *user settings*, not passing transient objects around between pieces of your program. Just use a property on an object that both have a reference to for that. – Catfish_Man Mar 19 '13 at 07:32

5 Answers5

2

Zohaib, there is conversion problem in your code.
First convert you image in "NSDATA" and then add to "NSUserDefaults"

NSData *data = [NSData dataWithData:UIImageJPEGRepresentation(image);

then you can get the data back.

iEngineer
  • 1,319
  • 1
  • 11
  • 27
  • Thanks it works, there were two problems 1) the one you told me to convert image in NSData first. 2) my image size was very big so i had to reduce it size and it works now. – Zohaib Mar 19 '13 at 07:45
  • This answer makes no sense. UIImageJPEGRepresentation returns an instance of NSData. The extra call to dataWithData doesn't "convert" the image to NSData any more than UIImageJPEGRepresentation already did. – Aaron Golden Mar 19 '13 at 07:52
  • @AaronGolden i have used UIImagePNGRepresentation. i have tried with dataWithData but it was not working even i have reduced my image size. but it works when i try dataWithdata. – Zohaib Mar 19 '13 at 07:58
1

This method will be called When you pick image from picker

So, take UIImage *imgGotFromPicker; declare this in .h file

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

    imgGotFromPicker = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}

When pushing send this image to other viewController.

In Second View Controller:

@property(nonatomic, strong) UIImage *imgfromView;  in .h

@synthesize imgfromView = _imgfromView;  in .m
Manu
  • 4,730
  • 2
  • 20
  • 45
1

To save the image and get it in any other view, you can use this method:

Save by:

[[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(image);

Retrieve by:

NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:key];
UIImage* image = [UIImage imageWithData:imageData];
Umit Kaya
  • 5,771
  • 3
  • 38
  • 52
0

If you have this image in your app, may be you should to save name of image? Store :

[[NSUserDefaults standardUserDefaults] setObject:@"bg.png"forKey:@"image"];

and after that in other view use this code

Read

UIImage *image = [UIImage imageNamed:[myUserDefaults valueForKey: @"image"]];
Vinodh
  • 5,262
  • 4
  • 38
  • 68
Arthur
  • 1,740
  • 3
  • 16
  • 36
  • i dont want to save image i have inside app. i can read it anywhere in my project. i want to save image which i am getting from imagepicker and save it and than use it in another viewcontroller – Zohaib Mar 19 '13 at 07:13
0

You are saving NSData into the NSUserDefaults directly. This is not the right process to do this . You need to use NSKeyedArchiver and NSKeyedUnarchiver for saving and retrieving data from NSUserDefaults. So please try this:

NSKeyedArchiver and NSKeyedUnarchiver

This link will help you to save and retrieve the data. You also need to implement these 2 methods:

-(void)encodeWithCoder:(NSCoder *)encoder
{

}

-(id)initWithCoder:(NSCoder *)decoder
 {

  return self;
}
Community
  • 1
  • 1
Rachit
  • 1,159
  • 3
  • 13
  • 23