Save images in NSUserDefaults?

Florik picture Florik · Jul 11, 2011 · Viewed 63.1k times · Source

Is it possible to save images into NSUserDefaults as an object and then retrieve for further use?

Answer

Bonny picture Bonny · Jul 11, 2011

To save an image in NSUserDefaults:

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

To retrieve an image from NSUserDefaults:

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