Basic Data - Image Saving (iPhone)

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

I want to know how to save an image (obtained from a camera or photo library) using Core Data strong>, since I can offer a backup tool for the user and will look for the data image to be transmitted to the server.

I came across a constructive proposal to create an Image model object and create a 1-to-1 relationship with a User model object (so that the associated Image object is not called if not required). However, I'm not sure how to practically save the image and whether it is potentially dangerous in terms of performance.

I would appreciate any advice on approach and traps from those who tried to do this.

+46
design iphone image core-data storage
Jan 18 '10 at 23:41
source share
4 answers

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

  • <100 kb in the corresponding object (person, address, whatever).
  • <1 mb to be stored in a separate object at the other end of the relationship to avoid performance problems.
  • Store 1 MB on disk and refer to the path in your master data store.

You can use the converted data type to store NSImage directly in Core Data. In fact, you can use the converted data type to store everything that implements the NSCoder protocol.

Personally, I would not convert it to CGImageRef, since you can lose a lot of information this way.

+84
Jan 20 '10 at 1:10
source share

The time elapsed since the request of this question, but I want to share my experience on this issue.


I would not recommend that you store images inside the database if their number is not limited. You should always remember that someday you will add a new version of the data model, and you will have to switch from the old database to the new version of the application. It takes time. The larger you DB file, the more migration is required.

If you want to store images in a database, do not add persistent storage to the application: didFinishLaunchingWithOptions: UIApplicationDelegate . iOS will terminate your application if the migration is not completed in XX seconds.

+29
Jul 02 '12 at 8:15
source share

Pitfall: you can get a huge, complex sqlite database to process. Do you really want your users to upload multiple MB files in one step to the server? What do you do if the celluar connection breaks for a few seconds?

I think it would be better if you used Core Data to manage your images and their download status (uploaded: yes or no). This way you can upload images when they fit your application workflow. Well, it will last a little longer, due to many connections. But I think this is a cleaner approach.

When you think about iTunes, when it comes to backups: in any case, your local iPhone Documents folder is synced.

+4
Jan 19 '10 at 0:08
source share

You should have aa, then you can 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 main data image object, and @ "data" is the name of the binary property. You may also need to save the image format in order to deserialize the image later.

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

+3
Jan 19
source share



All Articles