Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone-为什么文档中说UIImageView与NSCoding兼容?_Iphone_Uiimageview_Nscoding - Fatal编程技术网

iPhone-为什么文档中说UIImageView与NSCoding兼容?

iPhone-为什么文档中说UIImageView与NSCoding兼容?,iphone,uiimageview,nscoding,Iphone,Uiimageview,Nscoding,理想情况下,使用encodeWithCoder:和initWithCoder:(至少直到最近我还这么认为),符合NSCoding的类将按预期工作,而开发人员不必担心例程内部的情况(除非我对符合NSCoding的类的想法完全搞砸了!) UIImageView类与NSCoding兼容。因此,我不必担心如何使用NSKeyedArchiver和NSKeyedUnarchiver类对其进行序列化/反序列化。但每次我尝试对UIImageView对象进行编码时,都会遇到一个错误,UIImage无法识别Enco

理想情况下,使用encodeWithCoder:和initWithCoder:(至少直到最近我还这么认为),符合NSCoding的类将按预期工作,而开发人员不必担心例程内部的情况(除非我对符合NSCoding的类的想法完全搞砸了!)

UIImageView类与NSCoding兼容。因此,我不必担心如何使用NSKeyedArchiver和NSKeyedUnarchiver类对其进行序列化/反序列化。但每次我尝试对UIImageView对象进行编码时,都会遇到一个错误,UIImage无法识别EncoderWithCoder:method

现在UIImageView在内部使用UIImage对象。但是编码本身不应该考虑到这一点吗

或者文档中指定的NSCoding遵从性只是为了让用户知道他们可以实现initWithCoder和encodeWithCoder方法


有人能帮我澄清一下吗!我完全糊涂了

文档具有误导性--
UIImage
不符合您所述的
NSCoding
。您可以通过自己完成工作(以原始方式)来解决此问题:

@interface UIImage (NSCoding)
- (id)initWithCoder:(NSCoder *)decoder;
- (void)encodeWithCoder:(NSCoder *)encoder;
@end

@implementation UIImage (NSCoding)
- (id)initWithCoder:(NSCoder *)decoder {
  NSData *pngData = [decoder decodeObjectForKey:@"PNGRepresentation"];
  [self autorelease];
  self = [[UIImage alloc] initWithData:pngData];
  return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
  [encoder encodeObject:UIImagePNGRepresentation(self) forKey:@"PNGRepresentation"];
}
@end

这个问题值得更新,因为iOS 5.1增加了对UIImage进行NSCoding的功能,而Nathan de Vries的回答现在会在最新的编译器中引起警告


如果您的应用程序支持5.1之前的iOS,请解决此问题。它的功能基本上与Nathan的建议相同,但会检查该方法是否已经存在,而不是硬编码。

谢谢,我已经在这么做了。只是想知道(真的是咆哮)为什么苹果会误导开发者!与3.0相同,文档中没有提到UIImagePickerController中的新方法。一定要在Radar中提交一个文档错误(并将其添加到OpenRadar中,以便其他人可以跟踪进度)。谢谢Nathan de Vries。在处理CoreData时,您的回答对我帮助很大!在当时,这绝对是一个好的、正确的解决方案。然而,最近对iOS的一些更改影响了这种方法。在我发布的新答案中,我链接到了一个更详细的解释。这能正确地保存和加载图像的
scale
属性吗?很高兴我一直向下滚动页面!干杯,伙计