使用 NSUserDefaults 将大图像文件存储在二进制字符串中不成功,还有其他解决方案吗?

发布于 2024-12-17 05:34:40 字数 199 浏览 3 评论 0原文

我尝试放置我的自定义对象,该对象具有字符串和使用 NSUserDefaults 的图像存储,但我发现图像太大而无法存储。那么,还有其他解决方案来应对吗?我想以二进制格式存储它,而不是写入文件。

来自控制台的错误: * -[NSKeyedUnarchiver initForReadingWithData:]:数据为 NULL

谢谢。

I try to put my customized object, which have string and an image store using NSUserDefaults, but I found out that the image is too big that I can't store. So, any other solutions to deal with it? I would like to store it in a binary format, instead of writing a file.

The error from console:
* -[NSKeyedUnarchiver initForReadingWithData:]: data is NULL

Thanks.

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

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

发布评论

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

评论(3

檐上三寸雪 2024-12-24 05:34:40

另一种可能的解决方案是将其存储在核心数据中。在那里您可以指定将其保存为二进制数据。

Another possible solution is storing it in Core Data. There you can specify to save it as binary data.

離殇 2024-12-24 05:34:40

直接来自 Apple 文档

许多应用程序需要一种存储信息的机制
稍后会需要。对于需要存储的情况
少量持久数据——比如少于几百个
千字节属性列表提供了一种统一且方便的方法
组织、存储和访问数据。

在某些情况下,
属性列表架构可能被证明是不够的。如果你需要一个方法
存储大型、复杂的对象图,不支持的对象
属性列表架构,或者其可变性设置的对象
必须保留,使用归档。

你应该使用 NSKeyedArchiver 类。请参阅此链接保存用户创建的数据

directly from Apple Documentation.

Many applications require a mechanism for storing information that
will be needed at a later time. For situations where you need to store
small amounts of persistent data—say less than a few hundred
kilobytes property lists offer a uniform and convenient means of
organizing, storing, and accessing the data.

In some situations, the
property-list architecture may prove insufficient. If you need a way
to store large, complex graphs of objects, objects not supported by
the property-list architecture, or objects whose mutability settings
must be retained, use archiving.

you should use NSKeyedArchiver class. refer this link saving data created by the user.

彩虹直至黑白 2024-12-24 05:34:40

将此图像存储在您的文档目录中,并将路径保存在您的 NSUserDefault 中。

您可以像这样访问文档目录

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder

Store this image in your document directory and save the path in your NSUserDefault.

You can access to the document directory like that

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文