Baub
Baub

Reputation: 5054

Saving Photo in Core Data

I am trying to do this the most efficient way possible. Think of Apple's built in address book on the iPhone - that's what I'm trying to copy here.

One entity, Person, and multiple attributes, but each Person can have one photo.

What is the best way to save a photo for an entity?

Upvotes: 2

Views: 2247

Answers (3)

Mark Adams
Mark Adams

Reputation: 30846

There's a great new way to store binary data with Core Data in iOS 5.0+ and Mac OS X 10.7+. NSAttributeDescription now has a BOOL property called allowsExternalStorage.

From the documentation...

If this value is YES, the corresponding attribute may be stored in a file external to the persistent store itself.

This means that binary data will be persisted to the disk and a reference to that item will be stored in the persistent store automatically by Core Data. In the Xcode model editor, this can be enabled by simply checking the option in the inspector view for that attribute.

NSAttributeDescription Inspector - Allows External Storage


For versions before 5.0, you will need to save the image off to the user's Documents directory, and then add a file URL to your model object before saving.

Upvotes: 8

Ahti
Ahti

Reputation: 1430

In your managed object model file (.momd) in XCode, add an attribute to the Person entity and set it's type to "Transformable". In a transformable attribute, you can store any type of object which conforms to the NSCoding protocol and luckily, UIImage is one of those. For more information about NSCoding, see the Apple docs: http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Protocols/NSCoding_Protocol/Reference/Reference.html#//apple_ref/occ/intf/NSCoding

Upvotes: 1

Daniel
Daniel

Reputation: 22405

Best way for you to save photos or any large data in my opinion is save them to a file and store the file path as string in the core data entity...

Upvotes: 11

Related Questions