Learn Courses My Dashboard

Core Data Images

Who knows how to save a photo in core data in a View Controller and make it available to see in another View Controller?

1 Like

It is not recommended to save image data to CoreData as it bloats the database unnecessarily. The best method is to save the image to the Documents Folder and then save a reference (filename) to the image in CoreData.

The filename reference would ideally be a UUID()


If you set the allows external storage property on your binary data image attribute, then Core Data will decide whether to store the data in its tables or in the file system, depending on the size of the image involved. And you don’t need to mess around with figuring out the path or anything like that. I’m not sure what the exact threshold is, but I believe it’s around 100 kb or so; anything larger gets stored externally, everything else in the tables.

1 Like

How can we save the image into the Documents Folder? or what is the best practice for the user to take a photo or choose an image from the photo library and “save it” so it can be used inside our app by the user?? or in what lesson is that??

I’m really lost on this part. Thanks

See the post in this thread above yours where roosterboy suggests setting the allows external storage property on the binary image attribute. This is probably a better option.

Thanks. Yes, I tried, however when I fetch the image on a different ViewController from where the camera and photo library options are, the selected image sometimes appears upside down or sideways, it seems it does this depending on how I was holding my phone when I took the photo (although this last thing might just be coincidence). How could I solve this? I do not know if it might have something to do with me converting the photo into binary data via .pngData() and back into a UIImage with UIImage(data: ), and using CoreData to store the information? but I really want to solve this problem because having images appear in the UIImageViews upside down or sideways makes a very sloppy UX. On that note, do you know if there is an image rotation capability I could also include?. Thank you

I have seen that occur before and have no idea how to fix it.

Ok, thanks, no worries.