所以我试图将从UIImagePickerController中获取的图像保存到NSUserDefaults,这样我就可以在我生成的相册中打开它,但是当我调用委托方法保存图片时,我收到错误:
[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.`
代码如下:
self.savedPic = [[UIImage alloc] init];
self.savedPic = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[[NSUserDefaults standardUserDefaults] setValue:self.savedPic forKey:[NSString stringWithFormat:@"image_%i",imageCount]];
答案 0 :(得分:1)
您应该将图像保存到磁盘并将图像名称放在NSUserDefaults中,然后在需要时创建它:
[UIImage imageNamed:@"nameOfImage.png"]
答案 1 :(得分:1)
这很简单,请看我的代码:
- (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];
}