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
2009-12-15 20:49:32
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
2009-12-15 22:22:25
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
2009-12-16 09:06:40
Thank you so mush this helps my get what I wanted
Jaba
2009-12-16 12:47:50