iPhone:How can I store UIImage using NSUserDefaults?(iPhone:如何使用 NSUserDefaults 存储 UIImage?)
问题描述
我无法使用 nsuserdefaults 保存图像并使我的应用程序崩溃,并且我还使用了自定义类 UserInfo
I cannot save image using nsuserdefaults and crash my app and I also used custom class UserInfo which used
-(void)encodeWithCoder:(NSCoder *)encoder
{
NSData *imageData = UIImagePNGRepresentation(categoryImage);
[encoder encodeObject:categoryName forKey:@"name"];
[encoder encodeObject:imageData forKey:@"image"];
}
-(id)initWithCoder:(NSCoder *)decoder 和 userInfo.categoryImage 是 UIImage 变量.
-(id)initWithCoder:(NSCoder *)decoder and userInfo.categoryImage is UIImage variable.
原因:'-[UIImage encodeWithCoder:]:无法识别的选择器发送到实例 0x7676a70'
userInfo = [[UserInfo alloc] init];
userInfo.categoryName = categoryName.text;
NSLog(@"Category Name:--->%@",userInfo.categoryName);
userInfo.categoryImage = image;
appDelegate.categoryData = [[NSMutableDictionary dictionaryWithObjectsAndKeys:
userInfo.categoryName, @"name",userInfo.categoryImage,@"image", nil] mutableCopy];
[appDelegate.categories addObject:appDelegate.categoryData];
NSLog(@"Category Data:--->%@",appDelegate.categories);
[appDelegate.categories addObject:userInfo.categoryName];
[appDelegate.categories addObject:userInfo.categoryImage];
[self saveCustomObject:userInfo.categoryName];
[self saveCustomObject:userInfo.categoryImage];
-(void)saveCustomObject:(UserInfo *)obj
{
appDelegate.userDefaults = [NSUserDefaults standardUserDefaults];
NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:obj];
[appDelegate.userDefaults setObject:myEncodedObject forKey:@"myEncodedObjectKey"];
}
所以请帮助我如何使用 nsusedefaults 存储和检索 uiimage ???
So Please help me how can i store and retrieve uiimage using nsusedefaults???
推荐答案
UIImage 没有实现 NSCoding 协议,这就是你报错的原因.
UIImage does not implement NSCoding protocol, this is why you have the error.
商店:
NSData* imageData = UIImagePNGRepresentation(image);
NSData* myEncodedImageData = [NSKeyedArchiver archivedDataWithRootObject:imageData];
[appDelegate.userDefaults setObject:myEncodedImageData forKey:@"myEncodedImageDataKey"];
}
恢复:
NSData* myEncodedImageData = [appDelegate.userDefaults objectForKey:@"myEncodedImageDataKey"];
UIImage* image = [UIImage imageWithData:myEncodedImageData];
这篇关于iPhone:如何使用 NSUserDefaults 存储 UIImage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:iPhone:如何使用 NSUserDefaults 存储 UIImage?
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01