Core Data - Storing Images (iPhone) [closed]

巧了我就是萌 提交于 2019-11-26 11:31:37

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!