Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 499
Urizen Avatar asked Jan 18 '10 23:01

Urizen


People also ask

Can you store images in core data?

Is it possible to store UIImage in core data? Yes, we can store the images with the core data but not directly as UIImage. This can be achieved by converting UIImage to Data. Because UIImage is not a valid attribute type in Core Data.

Should I use Core Data iOS?

The next time you need to store data, you should have a better idea of your options. Core Data is unnecessary for random pieces of unrelated data, but it's a perfect fit for a large, relational data set. The defaults system is ideal for small, random pieces of unrelated data, such as settings or the user's preferences.

Where does core data store data?

The persistent store should be located in the AppData > Library > Application Support directory. In this example you should see a SQLite database with extension . sqlite. It is possible that you don't see the persistent store in the Application Support directory.

What is iOS core data?

Core Data is an object graph and persistence framework provided by Apple in the macOS and iOS operating systems. It was introduced in Mac OS X 10.4 Tiger and iOS with iPhone SDK 3.0. It allows data organized by the relational entity–attribute model to be serialized into XML, binary, or SQLite stores.


2 Answers

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.

like image 141
Marcus S. Zarra Avatar answered Oct 05 '22 00:10

Marcus S. Zarra


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.

like image 42
Eldar Markov Avatar answered Oct 05 '22 00:10

Eldar Markov