哪种方式存储数据(图像)? NSData,String或Transformable [英] Which way to store data(image)? NSData, String or Transformable

查看:162
本文介绍了哪种方式存储数据(图像)? NSData,String或Transformable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用base64编码。我已成功编码图像NSString从NSData,我也解码回NSData。



现在我想在Core Data中存储图像。但是最好存储一个NSString,NSData或第三个transformable?



将图像转换为NSString的原因是因为我想将其存储在XML中。



提前感谢。

解决方案

使用NSValueTransformer将图像转换为NSData对象,然后将数据blob存储在Core Data中。您可以在建模工具中注册您的值变换器子类。您可以查看Apple的PhotoLocations示例,也可以教程演示如何操作。



编辑完整性:因为其他人指出太大的数据blob会导致性能问题。正如@Jesse所指出的,iOS5有一个优化,其中如果数据blob太大,那么Core Data会将它存储在持久存储之外。如果您必须定位iOS5之前的版本,并且图片太大,那么您应该将该文件保存在沙箱中的某个位置,并将该URL存储在Core Data存储中。在Apple Dev论坛中的一个很好的讨论在这里,并讨论了在Core Data中存储数据Blob的限制。 p>

祝你好运


I have been working with base64 encoding. I have successfully encoded images to NSString from NSData, I have also decoded it back to NSData.

So right now I want to store images in Core Data. But would it be best to store an NSString, NSData or the third transformable?

The reason I convert images to NSString is because I want to store it in XML too.

Thanks in advance.

解决方案

Use an NSValueTransformer to convert from your image to an NSData object and then store the data blob in Core Data. You can register your value transformer subclass in the modeling tool. You can check out Apple's PhotoLocations example or this tutorial shows how.

Edit for completeness: as others have pointed out too large a data blob will cause performance issues. As pointed out by @Jesse, iOS5 has an optimization where if the data blob is too large, then Core Data will store it outside of the persistent store. If you have to target pre-iOS5 and the image is too large then you should save the file somewhere in the sandbox and store the URL in the Core Data store. A good discussion in the Apple Dev Forums is here and discusses the limits of storing data blobs in Core Data.

Good Luck

这篇关于哪种方式存储数据(图像)? NSData,String或Transformable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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