Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing photos and videos in Core Data?

I am developing an app that lets the user record videos and photos. Now I am wondering what is the best way to store them?

The first idea is of course to save it in the user's photo library, just as if he had recorded the photo from the Camera app, and use a reference to the files. Now there's the problem that the user can access and delete files from the Photos application, which may be still needed by my app. I don't think that I can prevent the user from deleting photos, but how do I deal with the inconsistency my app will suffer from?
The other way would be to take care of the storage myself. My app already uses Core Data, so I could convert the media files using an NSValueTransformer to an NSData object and store it as BLOB in Core Data. Then there are some more questions regarding the performance. I know that an SQLite-based Core Data can perfectly handle databases of several GB in size, but loading large data objects will take some time anyway. The obvious advantage is that the user is not going to make my app inconsistent, but is the performance penalty acceptable? And most of all: If I use a UIImagePickerController to capture photos and videos, it is going to save it in the library anyway. To save it in Core Data, I would have to take the photo from there, convert it to an NSData object, save it and delete it from the library again. This does not seem to be the right way to me. Also, UIKit provides me a function to get an NSData object from a photo via UIImageJPEGRepresentation(), but there seems to be no such function for converting videos.

All in all, because of all those unanswered questions, I would like to use the first approach. But the inconsistency problem puzzles me. What is the best practice to deal with it?

like image 750
Björn Marschollek Avatar asked Dec 13 '22 17:12

Björn Marschollek


2 Answers

For the photo:

UIImagePickerController will return us an UIImage inside the memory, so you wouldn't have to do anything, not taking it out of the user's photo library and save it in your core data...

For the video: You can take the data out using NSData *data = [NSData dataWithContentsOfFile:] or [NSData dataWithContentsOfUrl:] to get the NSData object of the video. You can get the videoUrl easily

So, I just corrected some of your wrong assumptions first.

You don't need to save it directly to CoreData. You can save it to your application's document folder and then users will not be able to delete it. Moreover, saving to the application's document folder and only save the link to CoreData will also save your soul from handling big data object.

The problem may be that you need to deal with your file storage.

I don't think there is a best practice here.

Saving in user's photo library will help the users see their photos and videos easier without accessing your app

Saving in CoreData may be easier for you and may be faster some time

Saving into file storage may help you not dealing with some binary problem when loading video because Apple MPMoviePlayerController may help you

For me, because I don't use much SQLite and Core Data, I choose to store into file system

like image 52
vodkhang Avatar answered Dec 29 '22 14:12

vodkhang


Use Code Data to store images and big files.

Code Data can save binary data to disk for you automatically, just set this option on the properties of the attribute, you just need to activate the option to allow external storage and Core Data will do the magic for you, simple as that.

enter image description here

Here you are the function to do in Swift3

//Image
let imageData = UIImageJPEGRepresentation(image, 1) // You can low compression up to >0

//Video from URL
let videoData = NSData(contentsOf:videoURL!)

Its so simple! iOs sweet programming.

like image 38
Kiko Seijo Avatar answered Dec 29 '22 12:12

Kiko Seijo