归档UIView对象,该对象将UIImageView和UIImage作为其在Objective-C中的子视图保存
我如何归档UIVIew,其中包含UIImagView和UIImage作为其子视图。 我试着这样做归档UIView对象,该对象将UIImageView和UIImage作为其在Objective-C中的子视图保存,objective-c,uiview,uiimageview,uiimage,archive,Objective C,Uiview,Uiimageview,Uiimage,Archive,我如何归档UIVIew,其中包含UIImagView和UIImage作为其子视图。 我试着这样做 data= [NSKeyedArchiver archivedDataWithRootObject: viewObject]; new_ViewObject= [NSKeyedUnarchiver unarchiveObjectWithData: data]; 但是程序崩溃了,因为[UIImage EncoderWithCoder:]:无法识别的选择器被发送到实例 我如何解决这个问题?如何在Obj
data= [NSKeyedArchiver archivedDataWithRootObject: viewObject];
new_ViewObject= [NSKeyedUnarchiver unarchiveObjectWithData: data];
但是程序崩溃了,因为[UIImage EncoderWithCoder:]:无法识别的选择器被发送到实例
我如何解决这个问题?如何在Objective-C中归档和取消归档此UIView对象
感谢您。UIView实现了NSCoding协议,因此您可以使用encodeWithCoder:获取序列化表示,并使用initWithCoder:从此类表示重新构建UIView。您可以在中找到许多详细信息 下面是如何执行此操作的快速示例:
- (NSData *)dataForView:(UIView *)view {
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:view forKey:@"view"];
[archiver finishEncoding];
[archiver release];
return (id)data;
}
- (UIView *)viewForData:(NSData *)data {
NSKeyedArchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
UIView *view = [unarchiver decodeObjectForKey:@"view"];
[unarchiver finishDecoding];
[unarchiver release];
return view;
}
请注意,如果我实际使用的是UIView的自定义子类,则需要覆盖encodeWithCoder:和initWithCoder:以添加您自己的属性