Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save images in NSUserDefaults?

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

like image 639
Florik Avatar asked Jul 11 '11 10:07

Florik


People also ask

How do you save an image in NSUserDefaults in Swift 4?

Ans : NSUserDefault support format NSData for save. So convert image into NSData and then save.

How do I save an image in NSUserDefaults in Objective C?

if let imageData = NSUserDefaults. standardUserDefaults(). objectForKey(kKeyImage), let image = UIImage(data: imageData as! NSData){ // use your image here... }

Can we save image in UserDefaults?

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.

What types can you store natively in NSUserDefaults?

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 .


1 Answers

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]; 
like image 55
Bonny Avatar answered Sep 19 '22 00:09

Bonny