NSUserDefault
的setObject 只能对少数 NSString等少数非自定义对象保存。
那么如果要保存自定义对象则需要。
使用[NSKeyedArchiver archivedDataWithRootObject:]归档以下将对象封装成NSData,再使用NSUserDefault保存。
使用时需要解压缩unarchiveObjectWithData
demo:
+(id)highImage{
NSData* data = [[NSUserDefaultsstandardUserDefaults] objectForKey:@"higt_image"];
id image= [NSKeyedUnarchiverunarchiveObjectWithData:data];
NSLog(@"image!:%@",image);
if(image){
return image;
}
return [UIImageimageNamed:@"Default.png"];
}
+(void)setHighImage:(UIImage*)image{
NSLog(@"image!:%@",image);
NSData* data=[NSKeyedArchiverarchivedDataWithRootObject:image];
[[NSUserDefaultsstandardUserDefaults] setObject:data forKey:@"higt_image"];
}
参考:http://blog.csdn.net/chyroger/article/details/5785297