• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - iOS6中使用restoreIdentifier保存UIImageView的状态

[复制链接]
菜鸟教程小白 发表于 2022-12-12 10:05:06 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我在文档中读到 @property(nonatomic, copy) NSString *restorationIdentifier 能够保留 UIImageView 属性的状态,例如位置、角度、等我尝试添加方法

-(BOOL)applicationUIApplication *)application shouldRestoreApplicationStateNSCoder *)coder
{
    return YES;
}

-(BOOL)applicationUIApplication *)application shouldSaveApplicationStateNSCoder *)coder
{
    return YES;
}

到 View Controller 。我已经在IB中将 View Controller 的恢复ID设置为@"myFirstViewController

我也在 View Controller 中添加了以下方法。

-(void)encodeRestorableStateWithCoderNSCoder *)coder
{
[coder encodeObject:_myImageView.image forKey"UnsavedImage"];
[super decodeRestorableStateWithCoder:coder];
}

-(void)decodeRestorableStateWithCoderNSCoder *)coder
{
_myImageView.image = [coder decodeObjectForKey"UnsavedImage"];
[super encodeRestorableStateWithCoder:coder];
}

我应该在 appDelegate 或 View Controller 中添加前两个方法吗? UIImageView 没有得到保留。这里有什么问题?



Best Answer-推荐答案


要使状态保存和恢复工作,始终需要两个步骤:

  • 应用代表必须选择加入
  • 每个 View Controller 或 View 要 保存/恢复必须分配一个恢复标识符。

您还应该为需要保存和恢复状态的 View 和 View Controller 实现 encodeRestorableStateWithCoder:decodeRestorableStateWithCoder:

将以下方法添加到 UIImageView 的 View Controller 中。

-(void)encodeRestorableStateWithCoderNSCoder *)coder
{
    [coder encodeObject:UIImagePNGRepresentation(_imageView.image)
                 forKey"YourImageKey"];

    [super decodeRestorableStateWithCoder:coder];
}

-(void)decodeRestorableStateWithCoderNSCoder *)coder
{
    _imageView.image = [UIImage imageWithData:[coder decodeObjectForKey"YourImageKey"]];

    [super encodeRestorableStateWithCoder:coder];
}

状态保存和恢复是一项可选功能,因此您需要通过实现两种方法让应用程序委托(delegate)选择加入:

- (BOOL)applicationUIApplication *)application shouldSaveApplicationStateNSCoder *)coder
{
    return YES;
}

- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
    return YES;
}

关于状态保存的有用文章: http://useyourloaf.com/blog/2013/05/21/state-preservation-and-restoration.html

关于ios - iOS6中使用restoreIdentifier保存UIImageView的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15872738/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap