1

我正在添加UIImageView按钮单击。我想使用UIKit. 我在以下位置获得恢复标识符:

 - (void)decodeRestorableStateWithCoder:(NSCoder *)coder;

我该如何解码UIImageView

4

2 回答 2

1

i have used this code in one of my app.

here is the encoding & decoding process

-(void)encodeRestorableStateWithCoder:(NSCoder *)coder
{

NSData *imageData=UIImagePNGRepresentation(self.imgViewProfilePicture.image);
[coder encodeObject:imageData forKey:@"PROFILE_PICTURE"];
[super encodeRestorableStateWithCoder:coder];
}

-(void)decodeRestorableStateWithCoder:(NSCoder *)coder
{

self.imgViewProfilePicture.image=[UIImage imageWithData:[coder decodeObjectForKey:@"PROFILE_PICTURE"]];
[super decodeRestorableStateWithCoder:coder];

}
于 2014-02-18T19:56:16.533 回答
1

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

  • 应用代表必须选择加入
  • 要保留/恢复的每个视图控制器或视图都必须分配一个恢复标识符。

您还应该为需要保存和恢复状态的视图和视图控制器实现encodeRestorableStateWithCoder:和。decodeRestorableStateWithCoder:

将以下方法添加到UIImageView.

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

    [super decodeRestorableStateWithCoder:coder];
}

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

    [super encodeRestorableStateWithCoder:coder];
}

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

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

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

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

于 2014-02-18T19:55:02.107 回答