Search code examples
objective-cios6ios7uikit-state-preservation

State Preservation and Restoration UIImageView added from button event


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?


Solution

  • 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];
    
    }