使用 NSCoding 归档 UIImageView
我以前从未使用过 NSCoding,并且对于应该如何实现它感到非常困惑。
我当前的 iPad 应用程序有一个 UIImageView(称为“背景”),它是我的主视图控制器的属性。 “background”有一个 UIImage “image”属性(显然)和用户添加的各种子视图。添加的子视图是我自己的 UIImageView 的自定义子类。
我需要能够保存“背景”UIImageView 的状态,以便可以使用相同的图像和所有子视图恢复存档时的状态。
我了解 UIImageView 符合 NSCoding 协议,但我不确定在哪里实现encodeWithCoder 和 initWithCoder。我是否从主视图控制器调用这些?我是否需要为 UIImageView 创建一个类别来允许我覆盖这些方法?
我是否需要编写代码来归档“背景”UIImageView 及其子视图的每个属性?我在其他地方读到过,UIImage 不符合 NSCoding,因此需要进行子类化或添加类别以便能够存档 UIImageView。
我认为有一种简单的方法可以将对象(包括其所有属性、子视图等)保存到磁盘。似乎需要做很多事情才能保存这个“背景” UIImageView 并在以后恢复它。我正在努力想象我需要做的一切。非常感谢任何指点!
I've never used NSCoding before and I'm very confused about how it should be implemented.
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.
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.
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?
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.
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
序列化(又名归档和取消归档)实际上相当复杂,但 Cocoa 让它变得简单的程度是一个令人印象深刻的壮举。
一旦你设置好 UIImageView 及其所有想要保持符合 NSCoding 的属性,那么保存该对象所需要做的就是:
然后将该 NSData 存储在某处。 取消归档该对象
然后,在从某处恢复 NSData 后
。至于让一切都符合NSCoding,
UIImageView
符合NSCoding
,UIView
也是如此,所以在你的UIImageView
之间,它的子视图及其属性,除了实际的 UIImage 之外,所有内容都可能符合 NSCoding 。为此,如果你用谷歌搜索,你可以找到很多人们为使其符合NSCoding
所做的类别,所以只需在你的项目中包含其中一个就可以了。Serialization (aka archiving and unarchiving) is actually pretty complicated, but the degree to which Cocoa makes it easy is a pretty impressive feat.
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:And then store that NSData somewhere. Then, to unarchive the object,
after recovering the NSData from somewhere.
As for making everything conform to NSCoding,
UIImageView
conforms toNSCoding
, as doesUIView
, so between yourUIImageView
, its subviews, and their properties, everything probably conforms toNSCoding
except for the actualUIImage
. For that, if you google you can find lots of categories people have made to make it conform toNSCoding
, so just include one of them in your project and you should be fine.