Saving Images to Core Data + iCloud - Swift

My app is a mood diary and saving the data I have chosen to use Core Data (for strings, images, etc.); so that the user can restore their diary, i have implemented iCloud which works well with Core Data.

Everything works well if I have few records, but when there are too many saved images, the application is slow to load data and detects memory warnings.

To save my images, I chose the converted data type, not binary data; I know this is not the best way to save images (better to save url), but I need to sync my data to iCloud, and saving images to Transformable allows me to sync data in an easy way (thanks to the ability offered by Apple Api to link master data and iCloud).

What can I do to avoid these storage warnings and sync my app photos to iCloud?

I've considered saving my photos to a custom photo album (if iCloud is activated for the Photos app, my photos to the app will sync), but I need to save them with a custom name to get them from the camera roll to my app, and for now moment i find no solution to save pic with custom name in custom photo album.

Storing photos in the document directory (and storing the urls in my main data object) would have been the right choice for a local database, but my app photos were not syncing.

+3


source to share


1 answer


There are several things you can try.

First, add an image thumbnail property that stores a smaller version of the image. Use this whenever possible. Loading a bunch of full-size photos takes a lot of memory, so change the load of smaller images when your user interface is down to size.

In addition, you can change the way images are processed using one of the following strategies. In ascending order of difficulty (and efficiency):



  • Make sure that "Allow External Storage" is enabled for the image property in your Data Model. This allows Core Data to push images outside of persistent storage without requiring you to manage those files. This will save memory if, for example, you sometimes retrieve data but don't use an image property.

  • Change the data model so that the image is stored in another object, with a relationship that links it to your current object. This should make it easier to "accidentally" load images when you're not using them.

  • Put the images in separate files and keep only the filenames in Core Data. You can still sync images via iCloud because you can sync files directly via iCloud outside of Core Data. But you will need some additional code to control the upload / download of images. You also need to make sure that you can handle the case where Core Data has finished syncing but the image is not yet available.

On this list, # 1 is easiest, but will probably have the least effect. Using # 3 should be very efficient, but it will take a lot of work.

+4


source







All Articles