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

发布于 2024-07-16 02:13:42 字数 945 浏览 4 评论 0原文

在我的应用程序中,我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只为守护你 2024-07-23 02:13:42

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

您可能想要做的是保留图像。 如果 self.currentpicture 是一个 'retain' 属性,它会自动释放前一个对象并保留新的对象,所以只需这样做:

self.currentpicture = img;

否则自己做:

[self.currentpicture release];
self.currentpicture = [img 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:

self.currentpicture = img;

Otherwise do it yourself:

[self.currentpicture release];
self.currentpicture = [img retain];

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.

美胚控场 2024-07-23 02:13:42

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

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

MyAppDelegate *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication]delegate];
UIImage *i = [appDelegate.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:

MyAppDelegate *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication]delegate];
UIImage *i = [appDelegate.currentPicture];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文