Core Data - Storing Images (iPhone) [closed]
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.
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.
Time passed since this question was asked but I want to share my experience regarding this issue.
I wouldn't recommend you storing images inside your DB if their number is not limited. You should keep in mind that someday you'll add a new version of the data model and you'll have to migrate from old DB to a new one on App update. It takes time. The bigger you DB file is longer it takes to migrate.
If you want to store images in DB be sure you don't add a persistent store in application: didFinishLaunchingWithOptions:
method of UIApplicationDelegate
. iOS will terminate your app if migration won't complete in XX seconds.
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.
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.