iPhone - 为什么文档说UIImageView符合NSCoding? [英] iPhone - Why does the documentation say UIImageView is NSCoding compliant?

查看:154
本文介绍了iPhone - 为什么文档说UIImageView符合NSCoding?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Ideally an NSCoding compliant class will work as expected using encodeWithCoder: and initWithCoder: (at least I thought so till recently) without the developer having to bother about what goes on inside the routines (unless my idea of an NSCoding compliant class are totally screwed up!)

UIImageView类符合NSCoding标准。所以我不应该打扰如何使用NSKeyedArchiver和NSKeyedUnarchiver类对它进行序列化/反序列化。但每次我尝试编码UIImageView对象时,都会收到UIImage无法识别的错误:encodeWithCoder:method。

The UIImageView class is NSCoding compliant. So I should not have to bother how it will be serialized/de-serialized using the NSKeyedArchiver and NSKeyedUnarchiver classes. But every time I try and encode a UIImageView object, I get an error that UIImage does not recognize encodeWithCoder: method.

现在UIImageView内部使用了一个UIImage对象。但编码是否应该自行处理?

Now the UIImageView internally uses a UIImage object. But shouldn't the encoding have taken care of that itself?

或者是文档中指定的NSCoding合规性,只是让用户知道他们可以实现initWithCoder和encodeWithCoder方法?

Or is the NSCoding compliance specified in the documentation to just let the user know that they can implement the initWithCoder and encodeWithCoder methods?

有人可以帮我解释一下这个问题!我很困惑!

Can someone please clarify this for me! I am thoroughly confused!

推荐答案

文档有误导性 - UIImage 如您所述,不符合 NSCoding 。您可以通过自己的工作来解决它(以原始的方式):

The documentation is misleading -- UIImage does not conform to NSCoding as you've stated. You can work around it (in a primitive way) by doing the work yourself:

@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

这篇关于iPhone - 为什么文档说UIImageView符合NSCoding?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆