0

In my app I will have an array of up to 50 images that people can maintain. They can choose to create new or delete existing images. Each image will have a few things associated with them, like a rating for example.

My question is how I should go about storing them. Should I create a CoreData entity called "Image" and store them that way? Should I set up a UIView subclass that conforms to NSCoding and just encode and decode the array and store it on the device? Is there another way I should consider? Thanks for any suggestions.

soleil
  • 12,133
  • 33
  • 112
  • 183

3 Answers3

0

You can create an entity that represents the image with its information, and use core data's external storage property for entity's attribute. This way, you get the convenience of core data without actually storing the images on the persistent store.

J2theC
  • 4,412
  • 1
  • 12
  • 14
  • I remember reading about that (such as http://stackoverflow.com/questions/7924840/storing-uiimage-in-core-data-with-the-new-external-storage-flag), but the implementation details of doing this are a bit cloudy to me. Do you know of any good links? – soleil Feb 26 '13 at 21:23
  • Is it just a matter of checking the "allow external storage" checkbox? So for all the maintenance when people add or remove images I'm still making calls via the CoreData API? – soleil Feb 26 '13 at 21:24
  • Core data will take care of it. Core data will store the image on an external file and do the background work for you when fetching the image. – J2theC Feb 26 '13 at 22:19
0

I had to make a similar decision recently and I decided to store the image in CoreData. I have a managed object called photo with a Binary Data field called image:

photo.image = UIImagePNGRepresentation(imageFile); // <- imageFile is a UIImage

And I recreate the image using:

[UIImage imageWithData:self.image];

One immediate advantage is that the images are deleted automatically with the object and there's no extra overhead in retrieving the image if you've already queried for the record.

Richard Brown
  • 11,346
  • 4
  • 32
  • 43
0

Core Data is probably overkill for what you want to do. Choose some key value pairs for descriptive information about the image. One key will be "path" and the value will be the path to the image file (or just its name). You can serialize array (or set) of dictionaries.

When your app starts up, read in the serialized array of dictionaries. Every time something changes, serialize and save the information. Write a short audit routine to insure that there is a one - to - one correspondence between dictionaries and images on file, and if one or the other is missing delete the other (will handle situations were you crash before getting to update something or the other).

Later on you can add more attributes to the dictionaries if you want, or even remove some.

[PS: I did this in a shipping app for one version, when the information needed to become relational I switched to Core Data.]

David H
  • 40,852
  • 12
  • 92
  • 138