0

I have EAGLview to display overlay image and taking screenshot then save this in NSUserDefaults for retrieve from viewcontroller.

But when i store the image into photo album it storing but it's not storing to NSUserDefaults. So, i'm getting NULL value.


UIImage *glImage = [self glToUIImage];
self.screensht = [self createSavableImage:glImage];

UIImageWriteToSavedPhotosAlbum(self.screensht, nil, nil, nil);


NSData* imgData1=UIImagePNGRepresentation(self.screensht);
[[NSUserDefaults  standardUserDefaults] setObject:imgData1 forKey:@"image"];
[[NSUserDefaults  standardUserDefaults]synchronize];


NSLog(@"nsuserdefauls is %@",[[NSUserDefaults standardUserDefaults] stringForKey:@"image"]);
Sergi Juanola
  • 6,531
  • 8
  • 56
  • 93
user3743552
  • 143
  • 3
  • 13

3 Answers3

0

Try this steps:

To Store UIImage:

  [[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation([UIImage imageNamed:@"yourimage.png"])forKey:@"your_key"];

To Retrive UIImage:

 NSData* imageData = [[NSUserDefaults standardUserDefaults]objectForKey:@"your_key"];
 UIImage* image = [UIImage imageWithData:imageData];
Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
0

You get nil because you wrongly use stringForKey to retrieve your object, which however is not a string. Try the following:

NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:key];

Also although technically correct I would not recommend storing images in a plist: read this answer for more details.

Community
  • 1
  • 1
tanz
  • 2,557
  • 20
  • 31
0

You are getting NULL Value because you stored NSData Object with the key "image". But you are trying to retrieve a NSString Object. You should use the same object type whenever you store and retrieve for the same key.

You can try something like

NSData* imgData2=[[NSUserDefaults standardUserDefaults] objectForKey:@"image"]
NSLog(@"nsuserdefauls is %@",imgData2);

You might see the object type from the above NSLog.

Ricky
  • 10,485
  • 6
  • 36
  • 49