将图像存储在Core Data或文件中?

时间:2021-11-03 16:59:53

I have set of data which contains images also. I want to cache this data. Should i store them on file system or on core data and why?

我有一组包含图像的数据。我想缓存这些数据。我应该将它们存储在文件系统还是核心数据上?为什么?

3 个解决方案

#1


36  

There are two main options:

有两个主要选择:

  1. Store the file on disk, and then store the path to the image in core data
  2. 将文件存储在磁盘上,然后将映像的路径存储在核心数据中
  3. Store the binary data of the image in core data
  4. 将图像的二进制数据存储在核心数据中

I personally prefer the 1st option, since it allows me to choose when I want to load the actual image in memory. It also means that I don't have to remember what format the raw data is in; I can just use the path to alloc/init a new UIImage object.

我个人更喜欢第一种选择,因为它允许我选择何时在内存中加载实际图像。这也意味着我不必记住原始数据的格式;我可以使用路径来分配/初始化一个新的UIImage对象。

#2


16  

You might want to read this from the Core Data Programming Guide on how to deal with binary large objects (BLOBs). There are rules of thumb for what size binary data should and should not be stored within the actual Core Data store.

您可能希望从“核心数据编程指南”中了解如何处理二进制大对象(BLOB)。对于什么尺寸的二进制数据应该和不应该存储在实际的Core Data存储中有经验法则。

You might also look at Core Data iPad/iPhone BLOBS vs File system for 20k PDFs

您还可以查看核心数据iPad / iPhone BLOBS与文件系统的20k PDF文件

If you do place binary data within Core Data store, you would do well to have a "Data" entity that holds the actual data and to have your "Image" entity separate. Create a relationship between the two entities, so that "Data" need only be loaded when actually needed. The "Image" entity can hold the meta-data such as title, data type, etc.

如果您确实在Core Data存储中放置二进制数据,那么最好有一个“数据”实体来保存实际数据并将“Image”实体分开。在两个实体之间创建关系,以便只需在实际需要时加载“数据”。 “图像”实体可以保存元数据,例如标题,数据类型等。

#3


0  

With regards to where to store the user data/files (I found "application support" to be a decent location given that i was wary of the user moving, deleting or altering the file in some way that would result in the image not being able to be recovered and used later by my application)

关于存储用户数据/文件的位置(我发现“应用程序支持”是一个不错的位置,因为我担心用户移动,删除或更改文件会导致图像无法移动待我的申请恢复和使用)

Take minecraft as an example: eg. "~/Library/Application Support/minecraft/saves/"

以Minecraft为例:例如。 “〜/ Library / Application Support / minecraft / saves /”

I would agree with the previous comments and store paths to the images in core data but otherwise store the images themselves as png files in their own folder outside of core data.

我同意先前的注释和存储核心数据中图像的路径,但是否则将图像本身作为png文件存储在核心数据之外的自己的文件夹中。

#1


36  

There are two main options:

有两个主要选择:

  1. Store the file on disk, and then store the path to the image in core data
  2. 将文件存储在磁盘上,然后将映像的路径存储在核心数据中
  3. Store the binary data of the image in core data
  4. 将图像的二进制数据存储在核心数据中

I personally prefer the 1st option, since it allows me to choose when I want to load the actual image in memory. It also means that I don't have to remember what format the raw data is in; I can just use the path to alloc/init a new UIImage object.

我个人更喜欢第一种选择,因为它允许我选择何时在内存中加载实际图像。这也意味着我不必记住原始数据的格式;我可以使用路径来分配/初始化一个新的UIImage对象。

#2


16  

You might want to read this from the Core Data Programming Guide on how to deal with binary large objects (BLOBs). There are rules of thumb for what size binary data should and should not be stored within the actual Core Data store.

您可能希望从“核心数据编程指南”中了解如何处理二进制大对象(BLOB)。对于什么尺寸的二进制数据应该和不应该存储在实际的Core Data存储中有经验法则。

You might also look at Core Data iPad/iPhone BLOBS vs File system for 20k PDFs

您还可以查看核心数据iPad / iPhone BLOBS与文件系统的20k PDF文件

If you do place binary data within Core Data store, you would do well to have a "Data" entity that holds the actual data and to have your "Image" entity separate. Create a relationship between the two entities, so that "Data" need only be loaded when actually needed. The "Image" entity can hold the meta-data such as title, data type, etc.

如果您确实在Core Data存储中放置二进制数据,那么最好有一个“数据”实体来保存实际数据并将“Image”实体分开。在两个实体之间创建关系,以便只需在实际需要时加载“数据”。 “图像”实体可以保存元数据,例如标题,数据类型等。

#3


0  

With regards to where to store the user data/files (I found "application support" to be a decent location given that i was wary of the user moving, deleting or altering the file in some way that would result in the image not being able to be recovered and used later by my application)

关于存储用户数据/文件的位置(我发现“应用程序支持”是一个不错的位置,因为我担心用户移动,删除或更改文件会导致图像无法移动待我的申请恢复和使用)

Take minecraft as an example: eg. "~/Library/Application Support/minecraft/saves/"

以Minecraft为例:例如。 “〜/ Library / Application Support / minecraft / saves /”

I would agree with the previous comments and store paths to the images in core data but otherwise store the images themselves as png files in their own folder outside of core data.

我同意先前的注释和存储核心数据中图像的路径,但是否则将图像本身作为png文件存储在核心数据之外的自己的文件夹中。