Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone UIImagePickerController didFinishPickingImage:UIImage在视图控制器之间传输?_Iphone_Uiviewcontroller_Uiimage_Uiimagepickercontroller - Fatal编程技术网

iPhone UIImagePickerController didFinishPickingImage:UIImage在视图控制器之间传输?

iPhone UIImagePickerController didFinishPickingImage:UIImage在视图控制器之间传输?,iphone,uiviewcontroller,uiimage,uiimagepickercontroller,Iphone,Uiviewcontroller,Uiimage,Uiimagepickercontroller,在我的应用程序中,我有一个UIImagePickerController。选择图像后,“我的视图控制器”需要获取图像并将其传递给另一个视图控制器,该视图控制器被推送到self.navigationController上。但我总是会遇到错误或零争论,诸如此类的事情。如果您能告诉我此代码有什么问题,我将不胜感激: FirstViewController.m: - (void)imagePickerController:(UIImagePickerController *)picker didFini

在我的应用程序中,我有一个UIImagePickerController。选择图像后,“我的视图控制器”需要获取图像并将其传递给另一个视图控制器,该视图控制器被推送到self.navigationController上。但我总是会遇到错误或零争论,诸如此类的事情。如果您能告诉我此代码有什么问题,我将不胜感激:

FirstViewController.m:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
 self.currentpicture = [img copy];
 [self dismissModalViewControllerAnimated:YES];
 [self goNext];
}
-(void)goNext{
 SecondViewController *vc = [[SecondViewController alloc] initWithNibName:@"Second" bundle:nil];
 [vc giveMePicture:currentpicture];
 [self.navigationController pushViewController:vc animated:YES];
}
-(void)giveMePicture:(UIImage *)data {
 self.currentpicture=[data copy];
}
SecondViewController.m:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
 self.currentpicture = [img copy];
 [self dismissModalViewControllerAnimated:YES];
 [self goNext];
}
-(void)goNext{
 SecondViewController *vc = [[SecondViewController alloc] initWithNibName:@"Second" bundle:nil];
 [vc giveMePicture:currentpicture];
 [self.navigationController pushViewController:vc animated:YES];
}
-(void)giveMePicture:(UIImage *)data {
 self.currentpicture=[data copy];
}
它们都将currentpicture定义为UIImage*currentpicture

我现在应该将currentpicture作为一些数据,但它每次都崩溃!我尝试过很多不同的事情,但我无法理解这一点

在不同视图控制器类之间共享数据的一种方法是通过应用程序委托

例如,您可以在应用程序委托中定义
UIImage*currentPicture
,然后在两个视图控制器类中进行访问,如下所示:

MyAppDelegate *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication]delegate];
UIImage *i = [appDelegate.currentPicture];

如果我错了,请纠正我,但UIImage不符合NSCopying,因此您无法成功复制它

您可能想做的是保留图像。如果self.currentpicture是“retain”属性,它将自动释放以前的对象并保留新对象,因此只需执行以下操作:

self.currentpicture = img;
否则,请自己动手:

[self.currentpicture release];
self.currentpicture = [img retain];
在这两种情况下,当您不再需要图像时,仍然必须调用[self.currentpicture release]。通常,您会在“self”对象的dealoc方法中执行此操作