使用NSCoding存档UIImageView [英] Archiving UIImageView with NSCoding

查看:118
本文介绍了使用NSCoding存档UIImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前从未使用过NSCoding,我对它应该如何实现感到非常困惑。

I've never used NSCoding before and I'm very confused about how it should be implemented.

我当前的iPad应用程序有一个UIImageView(称为背景) )这是我的主视图控制器的属性。 background具有UIImageimage属性(显然)和用户添加的各种子视图。添加的子视图是我自己的UIImageView自定义子类。

My current iPad app has a UIImageView (called "background") which is a property of my main view controller. "background" has a UIImage "image" property (obviously) and various subviews which are added by the user. The added subviews are my own custom subclasses of UIImageView.

我需要能够保存背景UIImageView的状态,以便可以使用相同的图像进行恢复以及存档时的所有子视图。

I need to be able to save the state of the "background" UIImageView so it can be restored with the same image and all the subviews in place as it was when archived.

我理解UIImageView符合NSCoding协议,但我不确定在哪里实现encodeWithCoder和initWithCoder。我是从主视图控制器调用这些吗?我是否需要为UIImageView创建一个允许我覆盖这些方法的类别?

I understand UIImageView conforms to the NSCoding protocol, but I'm not sure where to implement encodeWithCoder and initWithCoder. Do I call these from my main view controller? Do I need to create a category for UIImageView which allows me to override these methods?

我是否需要编写代码来存档我的背景UIImageView的每个属性及其子视图?我已经在其他地方读过UIImage不符合NSCoding,因此需要进行子类化或添加类别以便能够存档UIImageView。

Do I need to write code for archiving every property of my "background" UIImageView and its subviews? I have read elsewhere on SO that UIImage does not conform to NSCoding so needs to be subclassed or have a category added in order to be able to archive UIImageView.

我想在那里将一个简单的方法保存到磁盘一个对象,包括其所有属性,子视图等。似乎有很多需要做的事情,以便我保存这个背景UIImageView并在以后恢复它。我正在努力想象我需要做的一切。任何指针都非常赞赏!

I thought there would be a simple way to save to disk an object including all its properties, subviews etc. It seems there's a lot that needs to be done in order for me to save this "background" UIImageView and restore it later. I'm struggling to visualise everything I need to do. Any pointers much appreciated!

推荐答案

序列化(又名归档和取消归档)实际上非常复杂,但Cocoa的程度是多少easy是一个令人印象深刻的壮举。

Serialization (aka archiving and unarchiving) is actually pretty complicated, but the degree to which Cocoa makes it easy is a pretty impressive feat.

一旦你进行了设置,你想要保持的UIImageView及其所有属性都符合 NSCoding ,然后您需要做的就是保存对象:

Once you've set things up so that the UIImageView and all of its properties that you want to keep conform to NSCoding, then all you have to do to save the object is:

NSData *dataToSave = [NSKeyedArchiver archivedDataWithRootObject:yourImageView];

然后将NSData存储在某个地方。然后,要取消归档该对象,

And then store that NSData somewhere. Then, to unarchive the object,

UIImageView *restoredImageView = [NSKeyedUnarchiver unarchiveObjectWithData:dataToRestore];

至于使一切符合NSCoding, UIImageView 符合 NSCoding UIView ,所以在你的 UIImageView ,它的子视图和它们的属性之间,一切都可能符合 NSCoding 除了实际的 UIImage 。为此,如果你谷歌你可以找到许多类别,使其符合 NSCoding ,所以只需在你的项目中包含其中一个,你应该没事。

As for making everything conform to NSCoding, UIImageView conforms to NSCoding, as does UIView, so between your UIImageView, its subviews, and their properties, everything probably conforms to NSCoding except for the actual UIImage. For that, if you google you can find lots of categories people have made to make it conform to NSCoding, so just include one of them in your project and you should be fine.

这篇关于使用NSCoding存档UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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