tags:

views:

70

answers:

1

hi all, I am developing an application in which i take nsdata and convert it into image an display it in the image view.Now i want to save this data and display it again in imageview. I used nsuserdefaults to save the nsdata and again retreive this data and convert into image and display it in image view.But i am not getting the image back.my code is as follows:-

//for saving nsdata using nsuserdefaults method:- NSUserDefaults *defsMVC=[NSUserDefaults standardUserDefaults]; [defsMVC setObject:stateMVC forKey:@"MapImageObject"];

//for retrieving nsdata from nsuserdefaults and display it in imageview NSUserDefaults *defsMVC=[NSUserDefaults standardUserDefaults];

    NSData *stringmapdata=[defsMVC dataForKey:@"MapImageObject"];

    UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 6, 320,415)];
    UIImage *image = [UIImage imageWithData:stringmapdata];
    [imageView setImage:image];
    [image release];
    [self.view addSubview:imageView];
    [self.view sendSubviewToBack:imageView];
    [imageView release];
A: 

I don't think you should be storing the data in the user Defaults, try storing it in a file like "yourimage.png", then you can simply do [[NSImage alloc] initWithConentsOfFile:path];

Also try using NSArchiver before saving it in the user defaults:

NSImage *image = [NSUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"someKey"]];

And for archiving it to the user defaults:

[[NSUserDefaults standardUserDefaults] setObject:[NSArchiver archivedDataWithRootObject:image] forKey:@"someKey"];

Gotta love Cocoa =)

Antwan van Houdt
Are `NSUnarchiver` and `NSArchiver` available on iOS?
Whisty
Yes, /System/Library/Frameworks/Foundation.framework
Antwan van Houdt