Search code examples
iosobjective-cuiimagesavensdata

iOS saving UIImage in plist via file or via nsdata?


I am trying to save a Dictionary in a plist. One of the dictionary keys is an image.

I have two options (both tried and work), though not exactly sure which is better?

1) Saving the image in Documents folder as file + saving filename in plist for later retrieval?

2) Saving the NSData of the image directly in the plist for later retrieval?

I have tried both methods, and both work just fine. I am able to save and retrieve the images.

My question is: are there any pros / cons for one way over the other?

Thanks

EDIT:

more clarification: the images are from the camera, and I save them using

NSData *imageData  = UIImageJPEGRepresentation(image, 0.7f);
[imageData writeToFile:[SAD.myE documentsPathForFileName:@"testImage.jpg"] atomically:YES]; //Write the file

and I then scale them down, and save a smaller image to be used in a tableView, one image per cell.

As I understand, it is better to save them as files in Documents directory and retrieve them that way in the background?


Solution

  • It really depends on the way you save and more importantly, the way you consume the data later on.

    Saving in most cases can be offloaded to the background, then it is not your bottleneck and you are free to do whatever you want

    Reading the data often be a subject of a time constrains e.g. you are displaying pictures in a table view, this means you want to separate one image per file so you can read as atomic as possible. Or you might need random access - then you are better off by serializing all the data except for binary blobs into SQLite

    All and all if you can give an sample of data consumption.

    To clarify, if you are optimizing for reading - the best option is to save decompressed image so it won't even have to parse png or jpg format