I am adding UIImageView
on a button click. I want to restore it using UIKit
.
I am getting restoration identifier in:
- (void)decodeRestorableStateWithCoder:(NSCoder *)coder;
How can I decode this UIImageView
?
I am adding UIImageView
on a button click. I want to restore it using UIKit
.
I am getting restoration identifier in:
- (void)decodeRestorableStateWithCoder:(NSCoder *)coder;
How can I decode this UIImageView
?
To make the state preservation and restoration work there are two steps that are always required:
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
i have used this code in one of my app.
here is the encoding & decoding process