1

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?

Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
Jawad Ali
  • 13,556
  • 3
  • 32
  • 49
  • you should encode & decode the image not imageview. in staterestoration process. – Pawan Rai Feb 18 '14 at 19:53
  • imageview in either not in my .h file nor in xib file. i am making and adding that imageview to view on button click.i need to reconstruct the imageview? – Jawad Ali Feb 18 '14 at 20:04
  • there is a solution for it to. you have to make a collection of images. encode & decode this collection. you should try this by your self. – Pawan Rai Feb 18 '14 at 20:11
  • how can i preserve the position of image views ? – Jawad Ali Feb 18 '14 at 20:15
  • you need a custom class , which will have two properties. 1st will hold the image 2nd will hold the position. make you custom object add them to mutable array & encode it . again while decoding you will get the collection of image that will hold the image & there position. try it – Pawan Rai Feb 18 '14 at 20:21
  • how can i save image transform ? in my app user can zoom pan scale the image – Jawad Ali Feb 19 '14 at 03:27
  • then my answer should be accepted> – Pawan Rai Feb 19 '14 at 06:13
  • last question .. is that what pic collage is doing ? any guess – Jawad Ali Feb 19 '14 at 12:12

2 Answers2

1

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

Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
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];

}
Pawan Rai
  • 3,434
  • 4
  • 32
  • 42