我已成功从照片库中获取图像并将其设置在按钮上,但现在我想在另一个视图中发送此图像,然后如何使用NSUserDefault发送此图像
如果你有,请提供代码。
提前预定...
答案 0 :(得分:2)
我们可以在从一个视图推送到另一个视图的同时传递此图像对象....考虑“imageToPass”是您的UIImage对象。在FirstViewController.m中使用此代码,按钮操作:
NextViewController *nextView=[[NextViewController alloc]init];
[nextView initialiseWithImage:imageToPass] ;
[self.navigationController pushViewController:nextView animated:YES];
[nextView release];
在“NextViewController.m”中使用以下代码:
-(void)initialiseWithImage:(UIImage*)image{
//Use the "image".`enter code here`
}
或者您也可以从NextViewController直接从NSUserDefault获取。
答案 1 :(得分:1)
你可以使用上面的NSuser默认代码,但NSUserDefault的问题是它总是存储值。你必须根据它来实施条件。并在使用后将其设为零。
答案 2 :(得分:0)
Use Below Code It's Easy to use
-(void)storeImageNSDefaults{
//here stored images in NSUserDefaults.
//where imageName is image named.
NSData* imgData=UIImagePNGRepresentation(imageName);
[[NSUserDefaults standardUserDefaults] setObject:imgData forKey:@"image"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
//getImage from Nsusedefaults
// in other ViewController
-(void)showImage{
NSData* image=[[NSUserDefaults standardUserDefaults] objectForKey:@"image"];
UIImage* img=[UIImage imageWithData:image];
//now you can use that img image on any imageview
}
It'll Definitely Work
But i'll suggest you use the praveen's code with one change.
nextView.imageName=imageToPass
in place of [nextView initialiseWithImage:imageToPass] ;