2

So I'm trying to save an image taken from UIImagePickerController to NSUserDefaults so I can open it up in an album I generate, but when I call the delegate method to save the picture, I get the error:

[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value '<UIImage: 0x1d0bf0>' of class 'UIImage'.  Note that dictionaries and arrays in property lists must also contain only property values.`

The code is as follows:

self.savedPic = [[UIImage alloc] init];
self.savedPic = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[[NSUserDefaults standardUserDefaults] setValue:self.savedPic forKey:[NSString stringWithFormat:@"image_%i",imageCount]];
4

2 回答 2

1

You should save to disk your image and put the name of your image in the NSUserDefaults, then when needed create it :

[UIImage imageNamed:@"nameOfImage.png"]
于 2012-06-10T10:16:56.593 回答
1

It is quite simple, see my codes:

- (UIImage *)backgroundImage {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData* myEncodedImageData = [defaults objectForKey:@"backgroundImageData"];
UIImage* image = [UIImage imageWithData:myEncodedImageData];
return image;
}

- (void)setBackgroundImage:(UIImage *)image {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData* imageData = UIImagePNGRepresentation(image);
[defaults setObject:imageData forKey:@"backgroundImageData"];
[defaults synchronize];
}
于 2012-09-28T04:00:44.273 回答