我尝试使用UIImages对数组进行编码。
然后我称之为“保存”功能
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"SavedUserData"];
[NSKeyedArchiver archiveRootObject: self toFile:filePath];
我的行例外
[encoder encodeObject:recipePhotoList forKey:@"recipePhotoList"];
//带图像的数组
-[UIImage encodeWithCoder:]: unrecognized selector sent to instance 0x1b1380
2012-03-23 11:59:43.694 fishmarket[261:1a0f] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImage encodeWithCoder:]: unrecognized selector sent to instance 0x1b1380'
在5+ iOS上它完美运行,在4.3我有这个问题。
答案 0 :(得分:0)
这是因为UIImage
在iOS 5之前不支持NSCoding
。检查iOS 5.0 API diff.
答案 1 :(得分:0)
用于iOS的编码/解码UIIMage的替代方案< 5.0
+ (void)saveImage:(UIImage*)image withName:(NSString*)imageName
{
if (image != nil)
{
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithString:imageName]];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
}
}
+ (UIImage*)loadImageNamed:(NSString*)imageName
{
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithString:imageName]];
UIImage* image = [UIImage imageWithContentsOfFile:path];
return image;
}
希望能帮到别人。