4

I have been working with base64 encoding. I have successfully encoded images to NSString from NSData, I have also decoded it back to NSData.

So right now I want to store images in Core Data. But would it be best to store an NSString, NSData or the third transformable?

The reason I convert images to NSString is because I want to store it in XML too.

Thanks in advance.

WYS
  • 1,637
  • 2
  • 16
  • 37

4 Answers4

8

Transformable (as per @timthetoolman) is the easiest way. If your images are large, though, as of iOS 5, the right way to do this is to use the "Binary Data" attribute type and choose "Allows External Storage" so that Core Data can store the large blob of data outside of the database. This can be much more efficient.

Jesse Rusak
  • 56,530
  • 12
  • 101
  • 102
7

Use an NSValueTransformer to convert from your image to an NSData object and then store the data blob in Core Data. You can register your value transformer subclass in the modeling tool. You can check out Apple's PhotoLocations example or this tutorial shows how.

Edit for completeness: as others have pointed out too large a data blob will cause performance issues. As pointed out by @Jesse, iOS5 has an optimization where if the data blob is too large, then Core Data will store it outside of the persistent store. If you have to target pre-iOS5 and the image is too large then you should save the file somewhere in the sandbox and store the URL in the Core Data store. A good discussion in the Apple Dev Forums is here and discusses the limits of storing data blobs in Core Data.

Good Luck

timthetoolman
  • 4,613
  • 1
  • 22
  • 22
  • I see, so the prefered way is to use NSValueTransformer. But wouldn't this make me unable to store the image in XML? – WYS Nov 26 '11 at 23:49
  • The subclass of NSValueTransfirmer u create can transform from one value to another. You need to write the code that does the transformation so uncan convert to/from which ever format u like! – timthetoolman Nov 26 '11 at 23:58
1

Target iOS5 or later -- Performant Image storage becomes trivial

Newer versions of Xcode and iOS now make storing images both easy and performant. You no longer have to choose to store your images in core data for convenience or in the file system for performance -- Core Data will take care of it for you.

  • UIImage now conforms to NSCoding in iOS 5. If you're able to target iOS 5 and later, you can just set the the managed object's attribute as Transformable and be done.
  • if you check the "Allows External Storage" option, Core Data will make the decision whether to store the BLOB in the managed object or as an external file -- all transparently to the developer. to cause larger images to be saved outside of your Core Data store
memmons
  • 40,222
  • 21
  • 149
  • 183
1

I don't think you should store them in core data, I tried that with images once and found it to be too slow. You should store the locations of the images in core data but just write the images to a file. You can do that as follows:

// JPEG
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];

// PNG
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];
Michael Frederick
  • 16,664
  • 3
  • 43
  • 58
  • Agreed. Unless they're literally 10s of kilobytes, the file system is the way to go storing only references in core data. Core data is an object store not a database. – uchuugaka Sep 20 '13 at 00:11