在 Core Data 中存储照片和视频?

发布于 2024-10-09 02:21:22 字数 760 浏览 0 评论 0原文

我正在开发一个应用程序,可以让用户录制视频和照片。现在我想知道存储它们的最佳方式是什么?

第一个想法当然是将其保存在用户的照片库中,就像他从相机应用程序记录照片一样,并使用对文件的引用。现在存在的问题是用户可以从“照片”应用程序访问和删除文件,而我的应用程序可能仍然需要这些文件。我不认为我可以阻止用户删除照片,但是我该如何处理我的应用程序将遇到的不一致问题?
另一种方法是自己管理存储。我的应用程序已经使用 Core Data,因此我可以使用 NSValueTransformer 将媒体文件转换为 NSData 对象,并将其作为 BLOB 存储在 Core Data 中。然后还有一些关于性能的问题。我知道基于 SQLite 的 Core Data 可以完美处理几 GB 大小的数据库,但加载大数据对象无论如何都需要一些时间。明显的优点是用户不会让我的应用程序不一致,但是性能损失可以接受吗?最重要的是:如果我使用 UIImagePickerController 来捕获照片和视频,它无论如何都会将其保存在库中。要将其保存在 Core Data 中,我必须从那里拍摄照片,将其转换为 NSData 对象,保存并再次从库中删除。这对我来说似乎不是正确的方法。另外,UIKit 为我提供了一个通过 UIImageJPEGRepresentation() 从照片中获取 NSData 对象的函数,但似乎没有这样的用于转换视频的函数。

总而言之,由于所有这些未解答的问题,我想使用第一种方法。但不一致的问题让我困惑。处理它的最佳做法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

茶花眉 2024-10-16 02:21:22

对于照片

UIImagePickerController将返回内存中的UIImage,因此您无需执行任何操作,无需将其从用户的照片库中取出并将其保存在核心数据中...

对于视频
您可以使用 NSData *data = [NSData dataWithContentsOfFile:][NSData dataWithContentsOfUrl:] 取出数据来获取视频的 NSData 对象。您可以轻松获取 videoUrl

所以,我只是先纠正您的一些错误假设。

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

问题可能是您需要处理文件存储。

我认为这里没有最佳实践。

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

保存在 CoreData 中可能对您来说更容易并且可能更快有时

保存到文件存储中可能会帮助您在加载视频时不必处理一些二进制问题,因为 Apple MPMoviePlayerController 可能会帮助您

对我来说,因为我不使用太多 SQLite 和 Core Data,所以我选择存储到文件系统中

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

半葬歌 2024-10-16 02:21:22

使用代码数据存储图像和大文件。

Code Data 可以自动为您将二进制数据保存到磁盘,只需在属性的属性上设置此选项,您只需激活允许外部存储的选项,Core Data 就会为您发挥作用,就这么简单。

输入图片这里的描述

这里是 Swift3 中要做的功能

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

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

它是如此简单! iOs 甜蜜的编程。

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.

满栀 2024-10-16 02:21:22

使用 CoreData,图像需要以这种方式压缩:

let imageData = UIImage(imageLiteralResourceName:"Dummy1").jpegData(compressionQuality: 1)

在此示例中,Dummy1 位于 Assets.xcassets 中。

Using CoreData the image need to be compressed this way:

let imageData = UIImage(imageLiteralResourceName:"Dummy1").jpegData(compressionQuality: 1)

In this example, the Dummy1 is in the Assets.xcassets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文