-1

i have an 4 imageview in that if click on 1st imageview image transfer to next drawable view here user can edit the image using drawpad. user can draw line or rect on that image i want to save that image to NSUserDefault and when user back the first screen replace that original image with previous selected image

Save image to NSUserDefault i used this code.

-(IBAction)SaveBtn:(UIBUttonSender*)sender
{
    [delegate.imagesDef setObject:UIImagePNGRepresentation(previewImageView.image) forKey:@"first"];

    NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:@"first"];
                 delegate.image = [UIImage imageWithData:imageData];

    NSLog(@"%@",[delegate.image description]);
    NSLog(@"%@",imageData);
    [delegate.imagesDef synchronize];
}

This code works fine image save. i log it but when i goes previous controller image not replaced

i used this code for set new edited image.

if([delegate.imagesDef objectForKey:@"first"])
    {

        NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:@"first"];
        delegate.image = [UIImage imageWithData:imageData];

        NSLog(@"%@",[delegate.image description]);
        NSLog(@"%@",imageData);

        NSLog(@"%@",delegate.image);

        FirstImageView.image=delegate.image;    
    }

image not changed how may i solve this.

Please help me out this.

thanks in advance.

Hemant Singh Rathore
  • 2,153
  • 1
  • 24
  • 38
Jitendra
  • 5,055
  • 2
  • 22
  • 42
  • You should use core data or sqlite database, because userdeafaults is used for store small variables not that big type of values. – Hemant Singh Rathore Sep 17 '13 at 05:35
  • 6
    You would rather store the link to the image (in a database or NSUserDefaults, depends on 1. how many and 2. is it really a user default setting?) and save the image as a file. – HAS Sep 17 '13 at 05:37

2 Answers2

5

You should not store an UIImage into the NSUserDefaults. NSUserDefaults is meant for small pieces of data but not a whole image. Just write the image to disk (into the documents directory) and save the path into NSUserDefaults.

Loading:

NSString *pathToFile;
UIImage *image;
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:pathToFile atomically:YES];
[[NSUserDefaults standardUserDefaults] setObject:pathToFile forKey:@"image"];

Restoring:

NSString *pathToFile = [[NSUserDefaults standardUserDefaults] objectForKey:@"image"];
NSData *imageData = [NSData dataWithContentsOfFile:pathToFile];
UIImage *image = [UIImage imageWithData:imageData];

But why aren't you just passing the UIImage object itself between your ViewControllers using properties?

Georg
  • 3,664
  • 3
  • 34
  • 75
  • But beware that the full path to Documents changes with each update of the app (there is a random-generated component in the app directory's path). Better to save the path relative to Documents. – Nicolas Miari Sep 17 '13 at 05:44
  • But you can always get the path to you documents directory. And the content there stays the same. – Georg Sep 17 '13 at 05:48
  • See: http://stackoverflow.com/questions/6907381/what-is-the-documents-directory-nsdocumentdirectory – Georg Sep 17 '13 at 05:48
0

First of all using NSUserDefault for this thing is not recommended.

Few alternatives :

1) you can assign this image to firstViewControllers property so that you could access it later if you have its object declared in secondViewController

2) You can send a NSNotification to firstViewController thought which you can send the image to firstViewController from secondViewController

Hope this will help you.

Prashant Nikam
  • 2,253
  • 4
  • 17
  • 29