Reputation: 1175
facing with one problem,How to store images in NSUserDefaults that image contains url like this (www.example.com/1.png),how to store that images help me please. Here is my code,
listBannerArray =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
imageArray = [listBannerArray valueForKey:@"mainbanner"];
NSLog(@"%@",imageArray);
NSUserDefaults *imageDefaults = [NSUserDefaults standardUserDefaults];
[imageDefaults setObject:imageArray forKey:@"kPrefKeyForMainBanner"];
NSLog(@"image :%@",[imageDefaults objectForKey:@"kPrefKeyForMainBanner"]);
Here listBannerArray is my total response and imageArray is my images array. Please help!
Thanks in advance.
Upvotes: 0
Views: 1112
Reputation: 91
You can store and retrieve an image from NSUserDefaults like this:
NOTE:-
1.If you have UIImage object then you need to use UIImagePNGRepresentation to store .png image.
2.For .jpg/.jpeg UIImageJPEGRepresentation( lossy compression)if you use a compressionQuality value smaller than 1.0, you can lose some image quality.
[[NSUserDefaults standardUserDefaults]setObject:UIImagePNGRepresentation(image) forKey:@"yourKey"];
To retrieve an image from NSUserDefaults you can get it from NSData:
NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:@"yourKey"];
UIImage* image = [UIImage imageWithData:imageData];
Upvotes: 0
Reputation: 654
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];
Upvotes: 3
Reputation: 2470
You can read how to do such thing in this post, but again - it's not the best practice to do.
For correct approach, check this, for example.
Upvotes: 1