0

I'm picking an image from gallery and converting it to NSData. Later on I'm storing it in CoreData and retrieving later. But if I pick 2 images from the gallery, I want to store the `NSData info of both of them to an array and store that array to CoreData. But how an array can be saved to CoreData that I'm not able to figure out. Did go through other SO posts dealing with similar issue but they weren't of much help.

Also please note..I cannot store the array to Userdefaults or Document directory in this scenario because there are other data associated with the image like name,qty etc. and they are all stored in coredata and they should later be displayed in tableview. If just the images are stored using some other method and the other details stored using core-data, there won't be proper data populated in the tableview..

  • This link can help you... https://stackoverflow.com/questions/4266205/saving-pictures-in-core-data-in-to-many-relationship-environment – Alfa Jan 18 '18 at 07:51

2 Answers2

2

Use entity as ImagesArray with relationship to many entities as Image. In its turn, the picture will have properties e.g. imageData (Binary Data), Name (String) etc. and relationship to one ImagesArray.

See example

Create ImagesArray entity with identifier or other properties for identification.

enter image description here

And then create Image entity with properties imageData (Binary Data), name (String) etc.

enter image description here

Entity ImagesArray will have relationship images to many entities Image.

Entity Image will have inverse relationship imagesArray to one entity ImagesArray

enter image description here

In code you will create set of entities Image with imageData, name etc.

Then create ImagesArray.

Add set of entities Image to ImagesArray's property (relationship) images.

Entity ImagesArray will have set of entities Image.

Alexey Kudlay
  • 525
  • 2
  • 14
0

In my opinion saving an image to CoreData is not a correct approach to save images. You should save them in the app sand box and then store in CoreData the path.
Saying that you can save an Array by using NSValueTransformer, you can find an example here. but probably the best approach is to create a relation between a User entity and Image entity One-to-Many.

Andrea
  • 26,120
  • 10
  • 85
  • 131
  • I did see that link already @Andrea but couldn't get much help from it.. –  Jan 18 '18 at 07:56