xcode 在 NSUserDefault 中保存图像并在其他 ViewController 中获取
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15493409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Save image in NSUserDefault and get in other ViewController
提问by Zohaib
I am saving image in nsuserdefaulf in my first viewcontroller where i am getting image from imagepicker
我正在我的第一个视图控制器中将图像保存在 nsuserdefaulf 中,我从图像选择器获取图像
[[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(image) forKey:@"image"];
and i am getting this image in next viewController like this
我在下一个 viewController 中得到这个图像
NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:@"image"];
UIImage* image = [UIImage imageWithData:imageData];
but when i am passing this image for use it giving me error but when i am using image like this than it works. ( bg.png is saved inside project)
但是当我传递这个图像以供使用时,它给了我错误,但是当我使用这样的图像时,它会起作用。( bg.png 保存在项目中)
UIImage *image = [UIImage imageNamed:@"bg.png"];
Whats the problem with my code. ?
我的代码有什么问题。?
回答by iEngineer
Zohaib, there is conversion problem in your code.
First convert you image in "NSDATA" and then add to "NSUserDefaults"
Zohaib,您的代码中存在转换问题。
首先在“NSDATA”中转换你的图像,然后添加到“NSUserDefaults”
NSData *data = [NSData dataWithData:UIImageJPEGRepresentation(image);
NSData *data = [NSData dataWithData:UIImageJPEGRepresentation(image);
then you can get the data back.
然后你可以取回数据。
回答by Manohar Perepa
This method will be called When you pick image from picker
当您从选择器中选择图像时将调用此方法
So, take UIImage *imgGotFromPicker;
declare this in .h file
所以,UIImage *imgGotFromPicker;
在 .h 文件中声明这个
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
imgGotFromPicker = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}
When pushing send this image to other viewController.
推送时将此图像发送到其他 viewController。
In Second View Controller:
在第二个视图控制器中:
@property(nonatomic, strong) UIImage *imgfromView; in .h
@synthesize imgfromView = _imgfromView; in .m
回答by Umit Kaya
To save the image and get it in any other view, you can use this method:
要保存图像并在任何其他视图中获取它,您可以使用此方法:
Save by:
保存方式:
[[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(image);
Retrieve by:
检索方式:
NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:key];
UIImage* image = [UIImage imageWithData:imageData];
回答by Arthur
If you have this image in your app, may be you should to save name of image? Store :
如果您的应用程序中有此图像,您是否应该保存图像名称?店铺 :
[[NSUserDefaults standardUserDefaults] setObject:@"bg.png"forKey:@"image"];
and after that in other view use this code
然后在其他视图中使用此代码
Read
读
UIImage *image = [UIImage imageNamed:[myUserDefaults valueForKey: @"image"]];
回答by Rachit
You are saving NSData into the NSUserDefaults directly. This is not the right process to do this . You need to use NSKeyedArchiver and NSKeyedUnarchiver for saving and retrieving data from NSUserDefaults. So please try this:
您将 NSData 直接保存到 NSUserDefaults 中。这不是执行此操作的正确过程。您需要使用 NSKeyedArchiver 和 NSKeyedUnarchiver 来保存和检索来自 NSUserDefaults 的数据。所以请试试这个:
NSKeyedArchiver and NSKeyedUnarchiver
NSKeyedArchiver 和 NSKeyedUnarchiver
This link will help you to save and retrieve the data. You also need to implement these 2 methods:
此链接将帮助您保存和检索数据。您还需要实现这两个方法:
-(void)encodeWithCoder:(NSCoder *)encoder
{
}
-(id)initWithCoder:(NSCoder *)decoder
{
return self;
}