如何在Core Data中存储图像? [英] How to store an image in Core Data?

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

问题描述

只是一个猜测:我做一个属性,使其类型二进制。但到底,我该如何使用呢?我想在后台有一个NSData?那么该属性实际上需要一个NSData?

Just a guess: I make an attribute and make it's type "binary". But in the end, how would I use that? I guess there is an NSData behind the scenes? So that attribute actually takes an NSData?

推荐答案

这个问题已经被问了很多次,答案有点复杂。

This question has been asked a number of times and the answer is a bit more complex.

对于二进制数据,你应该根据你要使用的数据的预期大小来确定如何存储它。规则是:

When it comes to binary data you should determine how to store it based on the expected size of data you are going to be working with. The rule is:


  • 小于100K;以二进制属性的形式存储在主表中

  • 小于1M;在辅助表中存储为二进制属性以避免过度提取

  • 大于1M;存储在磁盘上并将其文件路径存储在Core Data表中。

此外,当您存储图像时,建议将其存储为标准格式(如JPG或PNG)。通过使用transformable属性类型,当实际的存储是PNG表示时,你实际上可以使你的子类给出访问 UIImage 类的外观。我在bog post中详细介绍了可可是我的女朋友

In addition, when you are storing images, it is recommended to store them in a standard format such as JPG or PNG. By using the transformable property type you can actually have your subclass give the appearance of accessing the UIImage class when the actual store is a PNG representation. I go into this in detail in the bog post on Cocoa Is My Girlfriend.

磁盘上的二进制数据是因为缓存。 NSPersistentStoreCoordinator 将保留数据的缓存,以便当您的应用程序请求下一个对象时,它不需要退回到磁盘。此缓存工作真的很好。但是它是小,在iOS上非常小。如果你拉入一个大块的二进制数据,你可以轻松地吹灭整个缓存,你的整个应用程序受到极大的影响。

The reason behind storing > 1M binary data on disk is because of the cache. The NSPersistentStoreCoordinator will keep a cache of data so that when your app asks for the "next" object it doesn't need to go back out to disk. This cache works really well. However it is small, very small on iOS. If you pull in a big piece of binary data you can easily blow out that entire cache and your entire app suffers greatly.

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

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