归档UIView对象,此对象在Objective-C中将UIImageView和UIImage保留为其子视图 [英] Archiving UIView Object which holds UIImageView and UIImage as a subview of it in Objective-C

查看:107
本文介绍了归档UIView对象,此对象在Objective-C中将UIImageView和UIImage保留为其子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何存档具有UIImagView和UIImage作为其子视图的UIVIew. 我试图以这种方式做到这一点,

How do i Archive UIVIew which has UIImagView and UIImage as subview of it. I tried to do it in this way,

data= [NSKeyedArchiver archivedDataWithRootObject: viewObject];
new_ViewObject= [NSKeyedUnarchiver unarchiveObjectWithData: data];

但是程序崩溃,并说[UIImage encodeWithCoder:]:无法识别的选择器发送到实例.

But the program crashes saying that [UIImage encodeWithCoder:]:Unrecognized selector sent to instance.

我该如何解决这个问题?如何在Objective-C中存档和取消存档此UIView对象?

How do i solve this problem? How can i archive and unarchive this UIView Object in Objective-C?

谢谢.

推荐答案

UIView实现了NSCoding协议,因此您可以使用encodeWithCoder:获取序列化表示形式,并使用initWithCoder:从此类表示形式重构UIView.您可以在可可化序列化编程指南.

UIView implements the NSCoding protocol, so you can use encodeWithCoder: to get a serialized representation, and initWithCoder: to reconstitute a UIView from such a representation. You can find a lot of details in the Serializations Programming Guide for Cocoa.

这是一个简单的示例:

- (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:来添加自己的属性

Note that if I am actually using a custom subclass of UIView, you'll need to override encodeWithCoder: and initWithCoder: to add your own properties

这篇关于归档UIView对象,此对象在Objective-C中将UIImageView和UIImage保留为其子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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