0

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.

UdayKiran Pulipati
  • 6,579
  • 7
  • 67
  • 92
zhuber
  • 5,364
  • 3
  • 30
  • 63
  • try this link http://stackoverflow.com/questions/6238139/ios-download-and-save-image-inside-app – pankaj asudani May 17 '13 at 09:17
  • i got some code for storing only images but about that "pairing" information with image and storing it, to know which information belongs to which image, thats what I need. Thanks anyway. – zhuber May 17 '13 at 09:20
  • Are you talking about [storing image and its exif data](http://stackoverflow.com/a/5294574/1407017)? – Amar May 17 '13 at 10:23

3 Answers3

1
// 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"]]);
}
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
  • Thanks alot. This looks pretty well, will test it and let you know how it worked. Thanks. – zhuber May 17 '13 at 10:34
  • Just one more thing, please I really need. Now if i want to access that image from path and set it as background of UIImageView *imgView, how can I achieve that ? And how can I access that same image "date" (one you set as example) key ? Thank you. – zhuber May 17 '13 at 10:50
  • yes date is example key, to set as background i will update my code, but you should know key for that image which you want to set as background – Dipen Panchasara May 17 '13 at 11:20
  • Well, it will be like setting button image, so name of saved image from URL will probably be button_image. Please update so I can test and thank you alot !!!! Really helped. – zhuber May 17 '13 at 11:24
  • that mechanisam you have to develope by which name you want to store your image, so that you can easily identify it. – Dipen Panchasara May 17 '13 at 11:34
  • I think [url lastPathComponent] will always provide name as button_image, background_image and stuff, so if I enter that in place of "68051_10151509108346729_1731694342_s.png" I should be good ? Cant test now but will do it later when I'm back on my computer. – zhuber May 17 '13 at 11:41
  • yes, that is true, set your button image name instead that name – Dipen Panchasara May 17 '13 at 11:48
  • One and really last thing :D . I tried accessing specific date from plist but couldnt achieve that. How can I access date of "68051_10151509108346729_1731694342_s.png" image ? Thank you alot you really helped me. – zhuber May 17 '13 at 12:06
  • imageInfoDict[@"68051_10151509108346729_1731694342_s.png"][@"date"], it will give you `NSDate` object – Dipen Panchasara May 17 '13 at 12:14
  • Is this function (when using file manager) always overwriting whole stored data with new data, not just parts of same path? – zhuber May 18 '13 at 13:29
  • if you change the key then it will store new value otherwise overwrite old value – Dipen Panchasara May 19 '13 at 04:41
  • Hey Dipen, thank you a lot on your help. Since are pretty good at this stuff, is it possible to store and retrieve font (.ttf file)? – zhuber May 21 '13 at 09:07
  • yes its possible, just convert your font file to `NSData` and set it. you can do it using `NSKeyedArchive`. – Dipen Panchasara May 22 '13 at 04:00
0

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.

Devang
  • 320
  • 3
  • 12
0

You should definitely use CoreData and app documents folder like this:

  1. Download the data from the server: image and the other data
  2. Save your image in the app documents folder with a generated name (if you don't already have the image name form the server)
  3. Create a CoreData table (or multiple tables) depending on your needs and set there the image name, and the rest of the properties. 4 When you try to load the data, you can fetch data from the core data table and load the image from the documents directory.

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.

danypata
  • 9,895
  • 1
  • 31
  • 44