iPhone - 保存UIImageView状态 [英] iPhone - save UIImageView state
问题描述
我已经创建了UIImageView的子类,我正在处理子类本身内部对象的触摸。
I have created a subclass of UIImageView and I am handling the touches for its objects inside the subclass itself.
现在,当用户即将退出应用程序时想要保存图像的状态。由于图像上可能发生了多次转换(我正在保存数组中的所有转换),我希望能够在applicationWillTerminate中保存这些对象。
Now when the user is about to exit the app I want to save the state of the images. And as there are multiple transforms which might have taken place on the images (I am saving all the transforms in an array), I want to be able to save these objects in applicationWillTerminate.
有什么办法可以保存这些物品吗?或者我必须单独保存所有内容?如果我这样做,如何保存图像视图对象上发生的所有转换,直到用户退出?
Is there any way I can save these objects? Or do I have to save everything individually? If I do, how do I save all the transformations which have happened on the image view objects till the user exits?
谢谢。
推荐答案
UIImageView
符合 NSCoding
。您需要在 UIImageView
子类中实现 encodeWithCoder:
,以允许它被序列化。您使用传递的编码器序列化重要的成员变量,在这种情况下,您的图像然后调用超类' encodeWithCoder:
。
UIImageView
conforms to NSCoding
. You will need to implement encodeWithCoder:
in your UIImageView
subclass to allow it to be serialised. You use the passed encoder to serialise the important member variables, in this case your images then call the superclass' encodeWithCoder:
.
你在 initWithCoder中执行相反的操作:
你已经恢复了原来的状态。
You do the the inverse of this in initWithCoder:
an you've got your original state back.
如果您的图像是 UIImage
的实例,那么您将需要做更多的工作,因为它不符合 NSCoding
。您可以将图像数据转换为 NSData
对象并根据您的要求进行编码。以下是如何完成此操作的示例: http:/ /www.nixwire.com/getting-uiimage-to-work-with-nscoding-encodewithcoder/
If your images are instances of UIImage
you'll have a bit more work to do since it doesn't conform to NSCoding
. You might be able to get away with converting the image data to an NSData
object and encoding that depending on your requirements. Here's an example of how this can be done: http://www.nixwire.com/getting-uiimage-to-work-with-nscoding-encodewithcoder/
查看档案和序列化编程指南for Cocoa: http://developer.apple.com /iphone/library/documentation/Cocoa/Conceptual/Archiving/Archiving.html
Have a look at the Archives and Serializations Programming Guide for Cocoa: http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/Archiving/Archiving.html
这篇关于iPhone - 保存UIImageView状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!