在Core Data中存储照片和视频? [英] Storing photos and videos in Core Data?

查看:277
本文介绍了在Core Data中存储照片和视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发的应用程式可让使用者录制影片和相片。现在我想知道什么是最好的方式来存储他们?



第一个想法当然是将其保存在用户的照片库中,就像他已经记录了来自相机应用程序的照片,并使用对文件的引用。现在有一个问题,用户可以访问和删除照片应用程序,这可能仍然需要我的应用程序的文件。我不认为我可以阻止用户删除照片,但如何处理我的应用程序将遭受的不一致?

另一种方法是照顾存储自己。我的应用程序已经使用Core Data,因此我可以使用 NSValueTransformer 将媒体文件转换为 NSData 对象并存储作为核心数据中的BLOB。然后还有一些关于性能的问题。我知道基于SQLite的核心数据可以完美地处理几个GB的数据库,但加载大型数据对象将需要一些时间。显而易见的优点是,用户不会让我的应用程序不一致,但性能惩罚是否可以接受?最重要的是:如果我使用 UIImagePickerController 捕获照片和视频,它将被保存在库中。要将它保存在Core Data中,我必须从那里获取照片,将其转换为 NSData 对象,保存它并再次从库中删除它。这似乎不是对我正确的方式。此外,UIKit提供了一个函数,通过 UIImageJPEGRepresentation()从照片中获取 NSData 没有这样的转换视频的功能。



总而言之,因为所有这些未回答的问题,我想使用第一种方法。但不一致的问题困惑我。



UIImagePickerController会在内存中返回一个UIImage,所以你不需要做任何事情,不会把它从用户的照片库中保存在你的核心数据中...



对于视频
您可以使用 NSData * data = [NSData dataWithContentsOfFile :] [NSData dataWithContentsOfUrl:] ,以获取视频的NSData对象。



因此,我刚刚修正了一些错误的假设。



不需要直接保存到CoreData。您可以将其保存到应用程序的文档文件夹,然后用户将无法删除它。此外,保存到应用程序的文档文件夹,只保存到CoreData的链接也将保存你的灵魂处理大数据对象。



问题可能是,你需要处理



$ b

b

保存到用户的照片库将有助于用户更轻松地查看他们的照片和视频,而无需访问您的应用



CoreData中的保存可能会更容易,一些时间



保存到文件存储可能会帮助你处理一些二进制的问题,当加载视频,因为苹果MPMoviePlayerController可以帮助你



对我来说,因为我不使用太多的SQLite和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?

解决方案

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

这篇关于在Core Data中存储照片和视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆