我正在尝试将 UIImage 保存ABRecordRef
在我自己的NSCoding
符合类中。据我现在所知,这UIImage
不NSCoding
符合要求,因此我尝试使用以下解决方法NSData
:
- (id)initWithRecordRef:(ABRecordRef)record{
//Getting the image
if (ABPersonHasImageData(record)){
CFDataRef imageData = ABPersonCopyImageData(record);
image = [UIImage imageWithData:(NSData *) imageData];
CFRelease(imageData);
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder{
//encode NSData representation from UIImage
tempData = UIImagePNGRepresentation(image);
[aCoder encodeObject:tempData forKey:@"image"];
}
- (id)initWithCoder:(NSCoder *)aDecoder{
//image
tempData = [aDecoder decodeObjectForKey:@"image"];
image= [UIImage imageWithData:[tempData valueForKey:@"image"]];
[tempData release];
return self;
}
由于某种原因,当我尝试保存此类的实例时,程序会立即崩溃。我做错了什么,或者做得更好:我怎样才能完成我想要的?谢谢!