iPhone UIImagePickerController didFinishPickingImage:视图控制器之间的UIImage传输?
在我的应用程序中,我有一个 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];
}
SecondViewController.m:
-(void)giveMePicture:(UIImage *)data {
self.currentpicture=[data copy];
}
它们都将 currentpicture 定义为 UIImage *currentpicture;
我现在应该将当前图片作为一些数据,但它每次都会崩溃! 我尝试了很多不同的事情,但我无法弄清楚这一点。
In my app, I have a UIImagePickerController. When a image is selected, my view controller needs to get the image and pass it to another view controller, which is pushed onto self.navigationController. But I am always getting SEGFAULTS or nil arguments, and things like that. I would appreciate it if you could tell me what is wrong with this code:
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];
}
They both have currentpicture defined as a UIImage *currentpicture;
I now should have currentpicture as some data, but it crashes every time! I have tried many different things and I cannot figure this out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我错了,请纠正我,但 UIImage 不符合 NSCopying,因此你无法成功复制它。
您可能想要做的是保留图像。 如果 self.currentpicture 是一个 'retain' 属性,它会自动释放前一个对象并保留新的对象,所以只需这样做:
否则自己做:
在这两种情况下,当你不再需要图像。 通常您会在“self”对象的 dealloc 方法中执行此操作。
Correct me if I'm wrong, but UIImage does not conform to NSCopying, therefore you can't successfully copy it.
What you probably want to do is retain the image. If self.currentpicture is a 'retain' property, it will automatically release the previous object and retain the new one, so just do this:
Otherwise do it yourself:
In both cases you still have to call [self.currentpicture release] when you no longer need the image. Usually you would do that in the 'self' object's dealloc method.
在不同视图控制器类之间共享数据的一种方法是通过应用程序委托。
例如,您可以在应用程序委托中定义
UIImage *currentPicture
,然后在两个视图控制器类中进行访问,如下所示:One way to share data between different view controller classes is through the app delegate.
For example you could have
UIImage *currentPicture
defined in you app delegate and then access in both of your view controller class as follows: