Is it possible to save images into NSUserDefaults
as an object and then retrieve for further use?
Ans : NSUserDefault support format NSData for save. So convert image into NSData and then save.
if let imageData = NSUserDefaults. standardUserDefaults(). objectForKey(kKeyImage), let image = UIImage(data: imageData as! NSData){ // use your image here... }
It is, but it isn't possible to store an image as is in the user's defaults database. The defaults system only supports strings, numbers, Date objects, and Data objects. This means that you need to convert the image to a Data object before you can store it in the user's defaults database.
Types stored in NSUserDefaultsplist type can be stored by NSUserDefaults . These types are NSString(String) , NSArray(Array) , NSDictionary(Dictionary) (for both NSArray and NSDictionary types their contents must be property list objects), NSNumber(Int, Float, Double, Boolean) , NSDate and NSData .
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With