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.
第一个想法当然是将它保存在用户的照片库中,就像他从相机应用中记录了照片一样,并使用对文件的引用。现在有一个问题,用户可以从Photos应用中访问和删除文件,这可能是我的app仍然需要的。我不认为我可以阻止用户删除照片,但是我如何处理我的app会出现的不一致性呢?另一种方法是自己保管仓库。我的应用程序已经使用了Core Data,所以我可以使用NSValueTransformer将媒体文件转换成NSData对象,并将其存储为Core Data中的BLOB。然后还有一些关于性能的问题。我知道,基于sql的核心数据可以很好地处理几GB大小的数据库,但是加载大型数据对象仍然需要一些时间。明显的优势是用户不会让我的app不一致,但是性能损失是否可以接受?最重要的是,如果我使用UIImagePickerController来捕捉照片和视频,它会把它保存在库中。要将它保存到Core Data中,我必须从那里获取照片,将它转换为NSData对象,保存并再次从库中删除它。对我来说,这似乎不是正确的方式。另外,UIKit提供了一个函数,可以通过UIImageJPEGRepresentation()从照片中获取一个NSData对象,但是似乎没有这样的函数来转换视频。
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?
总之,由于所有这些未解答的问题,我想使用第一种方法。但矛盾的问题让我困惑。处理它的最佳实践是什么?
2 个解决方案
#1
9
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...
UIImagePickerController会返回一个UIImage在内存中,所以你不需要做任何事,不需要从用户的照片库中取出并保存在你的核心数据中…
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
对于本视频:您可以使用NSData *data = [NSData dataWithContentsOfFile:]或[NSData dataWithContentsOfUrl:]取出数据,以获取视频的NSData对象。你可以很容易地得到这个视频
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.
不需要直接保存到CoreData。您可以将它保存到应用程序的文档文件夹中,然后用户将无法删除它。此外,保存到应用程序的文档文件夹,并且只保存到CoreData的链接,也可以避免您的灵魂处理大数据对象。
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
保存到文件存储中可以帮助您在加载视频时避免处理一些二进制问题,因为Apple MPMoviePlayerController可能会帮助您
For me, because I don't use much SQLite and Core Data, I choose to store into file system
对于我来说,因为我不使用太多的SQLite和Core Data,所以我选择存储到文件系统中
#2
1
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.
代码数据可以自动为您将二进制数据保存到磁盘,只需在属性属性上设置此选项,您只需激活该选项即可允许外部存储,而Core Data将为您实现这一功能,就这么简单。
Here you are the function to do in Swift3
这是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.
它的那么简单!iOs编程。
#1
9
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...
UIImagePickerController会返回一个UIImage在内存中,所以你不需要做任何事,不需要从用户的照片库中取出并保存在你的核心数据中…
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
对于本视频:您可以使用NSData *data = [NSData dataWithContentsOfFile:]或[NSData dataWithContentsOfUrl:]取出数据,以获取视频的NSData对象。你可以很容易地得到这个视频
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.
不需要直接保存到CoreData。您可以将它保存到应用程序的文档文件夹中,然后用户将无法删除它。此外,保存到应用程序的文档文件夹,并且只保存到CoreData的链接,也可以避免您的灵魂处理大数据对象。
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
保存到文件存储中可以帮助您在加载视频时避免处理一些二进制问题,因为Apple MPMoviePlayerController可能会帮助您
For me, because I don't use much SQLite and Core Data, I choose to store into file system
对于我来说,因为我不使用太多的SQLite和Core Data,所以我选择存储到文件系统中
#2
1
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.
代码数据可以自动为您将二进制数据保存到磁盘,只需在属性属性上设置此选项,您只需激活该选项即可允许外部存储,而Core Data将为您实现这一功能,就这么简单。
Here you are the function to do in Swift3
这是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.
它的那么简单!iOs编程。