【发布时间】:2010-02-02 17:29:28
【问题描述】:
如何将 UIImage 存储在 NSDictionary 中?
【问题讨论】:
标签: objective-c iphone uiimage nsdictionary
如何将 UIImage 存储在 NSDictionary 中?
【问题讨论】:
标签: objective-c iphone uiimage nsdictionary
是的,你可以:
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
UIImage *img = ...;
[dict setObject:img forKey:@"aKeyForYourImage"];
UIImage *imgAgain = [dict objectForKey:@"aKeyForYourImage"];
【讨论】:
好的,我最近遇到了这个问题,上面的解决方案对我没有用。因此,如果其他人在这里找到解决相同问题的方法,这就是它的完成方式
NSDictionary *imageDictionary = @{
kImageKey: UIImageJPEGRepresentation(/*INSERT YOUR UIImage HERE*/,0.1)
};
// 0.1代表压缩质量0.0最低到1.0最高质量
然后从字典中取回图像使用
[UIImage imageWithData:[imageDictionary objectForKey: kImageKey]];
【讨论】:
NSDictionary 可以存储 UIImage 值,只要您不尝试将字典保存到 plist 文件。
您不能将 UIImage 对象存储在字典中。您必须将图像转换为 NSData 对象并将其存储在字典中。
NSData *imgData = UIImageJPEGRepresentation(yourImage, 0.0f);
[dictionary setObject:imgData forKey:@"your key"];
【讨论】:
NSDictionary 可以存储 UIImage 值,只要您不尝试将字典保存到 plist 文件。
[dictionary setObject:yourImage forKey:@"whatever key you want"];
未经测试;-)
【讨论】:
[dict setObject:image forKey:@"ImageToStore"];
【讨论】:
这取决于你想用你的 NSDictionary 做什么。虽然,是的,您可以将 UIImage 存储在 NSDictionary 中,但在需要符合某些标准的情况下,您可能无法使用所述字典对象。
例如,您可以使用 NSDictionary 作为standardUserDefaults 中的值,但前提是该字典中的每个对象都是属性列表对象。 UIImage 不是属性列表对象。 NSData 是,因此您可以使用 UIImageJPEGREpresentation 将您的 UIImage 转换为 NSData,并以这种方式存储它,如上所述。
【讨论】: