Reputation: 5534
I retrieve image and its information (origin and some other info) from web. How can I store that on iPhone ? I saw using NSFileManager for storing images, but how can I store image and some other information with it ? Should I use NSDictionary and keys like - image, origin, info and then store that dictionary to NSMutableArray and then store that array to NSFileManager ?? Or is there some other way ? And small hint on accessing those saved images and corresponding information would be nice.
Thanks.
Upvotes: 0
Views: 1321
Reputation: 13600
// Download Image from Remote Server
NSURL *url = [NSURL URLWithString:@"Your IMAGE URL HERE"];
NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"moon" ofType:@"jpg"]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libPath = [paths objectAtIndex:0];
NSString *filePath = [libPath stringByAppendingPathComponent:[url lastPathComponent]];
BOOL status = [data writeToFile:filePath atomically:YES];
// Save Image file to Library Directory, if success then store infor about it in file
if(status)
{
// If File Exist then read it otherwise creat new
NSMutableDictionary *imageInfoDict;
if([[NSFileManager defaultManager] fileExistsAtPath:[libPath stringByAppendingPathComponent:@"imageInfo.plist"]])
{
NSData *fileData = [NSData dataWithContentsOfFile:[libPath stringByAppendingPathComponent:@"imageInfo.plist"]];
imageInfoDict = [NSMutableDictionary dictionaryWithDictionary:[NSKeyedUnarchiver unarchiveObjectWithData:fileData]];
// To set As Background Load image from disc
// Read filename from Dictionary, but you must know the key which you want to get
NSString *fileName = imageInfoDict[@"68051_10151509108346729_1731694342_s.png"][@"imagePath"];
// Set your image as background
UIImage *image = [UIImage imageWithContentsOfFile:[libPath stringByAppendingPathComponent:fileName]];
}
else
imageInfoDict = [NSMutableDictionary dictionaryWithCapacity:0];
// Create Dictionary to store Single Image Info
NSDictionary *imageInfo = [NSDictionary dictionaryWithObjectsAndKeys:[url lastPathComponent], @"imagePath",
[url lastPathComponent], @"imageName",
[NSDate date], @"date",nil];
// Add Single Image Info to Main Dictionary
[imageInfoDict setValue:imageInfo forKey:[url lastPathComponent]];
// Convert Main info Dictionary to `NSData` to Save on Disc
NSData *fileData = [NSKeyedArchiver archivedDataWithRootObject:imageInfoDict];
// Save file to Disc
[fileData writeToFile:[libPath stringByAppendingPathComponent:@"imageInfo.plist"] atomically:YES];
// Read Info From File
NSLog(@"%@",[NSDictionary dictionaryWithContentsOfFile:[libPath stringByAppendingPathComponent:@"imageInfo.plist"]]);
}
Upvotes: 1
Reputation: 10175
You should definitely use CoreData and app documents folder like this:
I think this is the best and easiest way to do it if you are downloading the images just one time.
If you want to download different images or update images you could use the same process with the mention that you shouldn't save the images on the documents folder, you just have to use an AsyncImageDownload library (lots of them) to download the images and you have to store the image URL in your database.
Upvotes: 0
Reputation: 320
Many ways are there depending upong your requirements
1) SQLITE : Save you image meta data in a table along with the file name in a row
2) NSUserDefaults : If number of images is pretty less
3) Text File : Write you Data separated by a new line, which should also not be difficult, again depends on your requirements.
Hope this helps.
Upvotes: 0