在我的应用程序中,我有一个UIImagePickerController.选择图像后,我的视图控制器需要获取图像并将其传递给另一个视图控制器,该视图控制器被推送到self.navigationController.但我总是得到SEGFAULTS或nil参数,以及类似的东西.如果您能告诉我这段代码有什么问题,我将不胜感激:
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]; }
SecondViewController.m:
-(void)giveMePicture:(UIImage *)data { self.currentpicture=[data copy]; }
他们都将当前图片定义为UIImage*currentpicture;
我现在应该将当前图片作为一些数据,但每次都会崩溃!我尝试了很多不同的东西,我无法弄清楚这一点.
如果我错了,请纠正我,但是UIImage不符合NSCopying,因此你无法成功复制它.
您可能想要做的是保留图像.如果self.currentpicture是'retain'属性,它将自动释放前一个对象并保留新对象,所以只需这样做:
self.currentpicture = img;
否则自己动手:
[self.currentpicture release]; self.currentpicture = [img retain];
在这两种情况下,当您不再需要图像时,仍然需要调用[self.currentpicture release].通常你会在'self'对象的dealloc方法中做到这一点.