views:

200

answers:

1

How do I save an Image to User Defaults

+1  A: 

You can save it as NSData. To get a NSData object from a UIImage, you can use one of the following functions:

NSData * UIImagePNGRepresentation (UIImage *image);
NSData * UIImageJPEGRepresentation (UIImage *image);

And to turn the NSData back into an image, you can use imageWithData or initWithData

UIImage * a = [[UIImage alloc] initWithData:data];
UIImage * b = [UIImage imageWithData:data];

But maybe it's a better idea to write it to a different file.

Zydeco
Well then how do I write ti to the temp folder and if I want to overwrite then how do I do that, also how do I read that file back?Thank you for the code btw.
Jaba
You can save the NSData with one of the writeToFile methods NSData *data = UIImagePNGRepresentation(image); NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"file.png"]; [data writeToFile:filePath atomically:YES];
Zydeco
Thank you so mush this helps my get what I wanted
Jaba