To make the state preservation and restoration work there are two steps that are always required:
- The App delegate must opt-in
- Each view controller or view to be
preserved/restored must have a restoration identifier assigned.
You should also implement encodeRestorableStateWithCoder:
and decodeRestorableStateWithCoder:
for views and view controllers that require state to be saved and restored.
Add the following methods to the view controller of your 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];
}
State preservation and restoration is an optional feature so you need to have the application delegate opt-in by implementing two methods:
- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
{
return YES;
}
- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
return YES;
}
Useful article about state preservation:
http://useyourloaf.com/blog/2013/05/21/state-preservation-and-restoration.html