如何将 UIImage 存储在 NSDictionary 中?

发布于 2024-08-20 00:20:38 字数 38 浏览 7 评论 0原文

如何将 UIImage 存储在 NSDictionary 中?

How can I store a UIImage in an NSDictionary?

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

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

发布评论

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

评论(6

仅此而已 2024-08-27 00:20:38

是的,您可以:

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
UIImage *img = ...;
[dict setObject:img forKey:@"aKeyForYourImage"];

UIImage *imgAgain = [dict objectForKey:@"aKeyForYourImage"];

Yes you can:

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
UIImage *img = ...;
[dict setObject:img forKey:@"aKeyForYourImage"];

UIImage *imgAgain = [dict objectForKey:@"aKeyForYourImage"];
征棹 2024-08-27 00:20:38

好吧,我最近遇到了这个问题,上述解决方案对我来说从来没有用过。因此,如果其他人在这里找到解决同样问题的方法,那么它是如何完成的

NSDictionary *imageDictionary = @{
 kImageKey:  UIImageJPEGRepresentation(/*INSERT YOUR UIImage HERE*/,0.1)
                                 };

// 0.1 表示压缩质量 0.0 最低,1.0 最高质量,

然后从字典使用中获取图像

[UIImage imageWithData:[imageDictionary objectForKey: kImageKey]];

OK I had this problem recently and the above solutions never worked for me. So if someone else finds their way here to solve the same problem, here is how it's done

NSDictionary *imageDictionary = @{
 kImageKey:  UIImageJPEGRepresentation(/*INSERT YOUR UIImage HERE*/,0.1)
                                 };

// the 0.1 represents the compression quality 0.0 being lowest to 1.0 being the highest quality

then to get image back from dictionary use

[UIImage imageWithData:[imageDictionary objectForKey: kImageKey]];
阳光的暖冬 2024-08-27 00:20:38

您不能将 UIImage 对象存储在字典中。您必须将图像转换为 NSData 对象并将其存储在字典中。

NSData *imgData = UIImageJPEGRepresentation(yourImage, 0.0f);
[dictionary setObject:imgData forKey:@"your key"];

You cannot store UIImage object in a dictionary. You will have to convert the image into an NSData object and store that in dictionary.

NSData *imgData = UIImageJPEGRepresentation(yourImage, 0.0f);
[dictionary setObject:imgData forKey:@"your key"];
南冥有猫 2024-08-27 00:20:38

[dictionary setObject:yourImage forKey:@"无论你想要什么键"];

未经测试;-)

[dictionary setObject:yourImage forKey:@"whatever key you want"];

Untested ;-)

悲欢浪云 2024-08-27 00:20:38

[dict setObject:image forKey:@"ImageToStore"];

[dict setObject:image forKey:@"ImageToStore"];

瑾兮 2024-08-27 00:20:38

这取决于你想用你的 NSDictionary 做什么。虽然,是的,您可以将 UIImage 存储在 NSDictionary 中,但您可能无法在需要它符合某些标准的实例中使用所述字典对象。

例如,您可以使用 NSDictionary 作为 standardUserDefaults 中的值,但前提是该字典中的每个对象都是属性列表对象。 UIImage 不是属性列表对象。 NSData 是,因此您可以使用 UIImageJPEGREpresentation 将 UIImage 转换为 NSData,并以这种方式存储,如上所述。

It depends on what you want to do with your NSDictionary. While, yes, you can store a UIImage in NSDictionary, you may not be able to use said dictionary object in instances where you need it to conform to certain standards.

For example, you can use an NSDictionary as a value in standardUserDefaults, but only if every object in that dictionary is a property list object. UIImage is not a property list object. NSData is, so you can convert your UIImage to NSData with UIImageJPEGREpresentation, and store it that way, as mentioned above.

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