简体   繁体   English

iPhone-将NSCoder与UIImageView的子类一起使用

[英]iPhone - Using NSCoder with subclass of UIImageView

I have created a subclass of UIImageView and I am handling the touches for its objects inside the subclass itself. 我已经创建了UIImageView的子类,并且正在处理子类本身内部对象的触摸。

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 a dictionary), I want to be able to save these objects in applicationWillTerminate. 由于图像上可能发生了多种转换(我将所有转换都保存在字典中),因此我希望能够将这些对象保存在applicationWillTerminate中。

I am using the encodeWithCoder and initWithCoder methods. 我正在使用encodeWithCoder和initWithCoder方法。 The objects are saved and loaded as expected (at least looks like it). 对象将按预期方式保存和加载(至少看起来像它一样)。 But when I print the frame size and origin, I get some absurd values on the console. 但是,当我打印帧大小和原点时,在控制台上会得到一些荒谬的值。

Can someone please tell me if the frame is not saved when encodeWithCoder is called and the contents are stored in a file? 有人可以告诉我在调用encodeWithCoder并将内容存储在文件中时是否没有保存帧吗?

Thanks. 谢谢。

Why don't you serialize the UIImageView's frame along with the actual UIImageView? 为什么不将UIImageView的框架与实际的UIImageView一起序列化? Just add another property in encodeWithCoder and initWithCoder for the frame. 只需在帧的encodeWithCoder和initWithCoder中添加另一个属性。 This way, you can be sure to have the correct frame if you feel something is off. 这样,如果您感觉有些不对劲,就可以确保拥有正确的镜框。

Please paste the code where you are logging the values of the UIImageView's frame. 请将该代码粘贴到您要记录UIImageView框架的值的位置。 I believe you may be casting to the wrong type which will cause your values to be off. 我认为您可能使用了错误的类型,这将导致您的值被关闭。

通过使用NSUserDefault的key对象和setobject key方法将Dictionary内容写入plist

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM