views:

1142

answers:

3

I have an application where I allow the user to add an image for their account.

I wish to know how to store an image (obtained from the camera or photo library) using Core Data, as I may offer a backup facility for the user and would be looking to transfer the image data to a server.

I have come across the design suggestion of creating an Image model object and creating a 1-to-1 relationship with the User model object (so that the associated Image object is not called up unless required). However, I am unsure how to practically store the image and whether this is potentially fatal in terms of performance.

I would be grateful for any advice on the approach and pitfalls from anyone who has attempted this.

+2  A: 

You would have to have a a a, then you can could do something like this:

CGImageRef imageRef = uiImage.CGImage;
CGDataProviderRef dataProvider = CGImageGetDataProvider(imageRef);
NSData *imageData = (NSData*)CGDataProviderCopyData(dataProvider);
[managedObject setValue:imageData forKey:@"data"];

Where managedObject is your core data image object and @"data" is the name of the binary property. You might also need to save the image format in order to deserialize the image later.

Another option is to save the image to disk and store the path in core data.

Elfred
also, release imageData when you are done.
Elfred
Also check out using a Transformable Attribute: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CoreData/Articles/cdNSAttributes.html
Nimrod
Sorry, I know this might seem like a basic question but how do you go about (and what is meant by) saving the image format? I've not come accross this idea before.
Urizen
I'm refering to storing the fact that the image is a jpg or png (or some other format). You will need this info when you fetch the image data from the database in order to select the right data provider to convert it to a CGImageRef.
Elfred
Thanks Elfred. Very helpful answer.
Urizen
+1  A: 

Pitfall: You may end up huge, hard to handle sqlite database. Do you really want your users to upload a several MB file in one step to the server? What do you do, if the celluar connection breaks down for some seconds?

I think it would be better, if you use Core Data for managing your images and their upload state (uploaded: yes or no). This way, you can upload the images, when it fits in your application workflow. Okay, it will last a bit longer, because of the many connections. But is a cleaner approach, I think...

When you think about iTunes, when speaking of backups: Your local iPhone 'Documents' folder is synced anyway.

Stefan
Thank you for the answer. I may go down the route of simply storing the path as a string and then counting on iTunes to provide the backup of the image file (while storing the other account related data on the remote server).
Urizen
+12  A: 

The rule for storing image data in Core Data is as follows:

  • < 100 kb store in the related entity (person, address, whatever).
  • < 1 mb store in a separate entity on the other end of a relationship to avoid performance issues.
  • > 1 mb store on disk and reference the path in your Core Data store.

You can use the transformable data type to store the NSImage directly into Core Data. In fact you can use the transformable data type to store anything that implements the NSCoder protocol.

Personally I would not convert it to a CGImageRef as you can lose a lot of information that way.

Marcus S. Zarra