尝试显示从一个 viewController 捕获的 UIImage,并在 iOS 中的另一个视图中显示它 [英] Trying to display a UIImage captured from one viewController, and display it in another in iOS

查看:23
本文介绍了尝试显示从一个 viewController 捕获的 UIImage,并在 iOS 中的另一个视图中显示它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 iPad 应用程序,我正在处理它,从我的主视图控制器(称为 mainViewController)调用并显示另一个从 .xib 文件创建的视图控制器(称为 nextViewController).第二个 viewController 允许用户像这样捕获图像:

I have an iPad application that I am working on where, from my main viewController (call it mainViewController), I call and display another viewController (call it nextViewController) that is created from an .xib file. This second viewController allows the user to capture an image like so:

- (IBAction)imageCapture:(id)sender {

    _myImage = [_nextView captureImage];
    UIImageWriteToSavedPhotosAlbum(_myImage, nil, nil, nil);
    [self dismissViewControllerAnimated:YES completion:nil];

}

一旦这个图像被捕获,我现在需要这个图像被传递回调用视图控制器(mainViewController),但老实说我不知道​​如何做到这一点.我试图在我的 nextViewController 中创建我的 mainViewController 的属性引用,我试图这样做是为了将新获取的图像的引用传递给 mainController 的属性,但这不起作用.

Once this image is captured, I need this image to now be passed back to the calling viewController (mainViewController), but I honestly am not sure how to do this. I have tried to create a property reference of my mainViewController in my nextViewController, which I was trying to do in order to pass the reference of the newly acquired image to an attribute of the mainController, but this is not working.

提前感谢所有回复的人.

Thanks in advance to all who reply.

推荐答案

要有效地做到这一点,您应该不要在呈现的视图控制器中创建属性 mainViewController.这会导致可重用性问题,而且它的紧耦合是不可取的.相反,正如上面的评论所指出的,您应该使用协议.对于 Objective-C,请参阅本文档 用于协议语法.

To do this effectively, you should not make a property mainViewController in the presented view controller. This would cause reusability issues, plus it's tight coupling which is not desirable. Instead, as the comments above note, you should use a protocol. For Objective-C, see this documentation for protocol syntax.

在你的 NextViewController 中,你应该创建一个这样的委托方法:

In your NextViewController, you should make a delegate method something like this:

-(void)nextViewController:(NextViewController *)nextViewController capturedImage:(UIImage *)image;

然后,在你的主视图控制器类中,你应该实现这个方法并对图像做任何你想做的事情.值得注意的是,您可能希望从主视图控制器中关闭控制器.创建时不要忘记设置下一个视图控制器的委托属性.

Then, in your main view controller class, you should implement this method and do whatever you want with the image. Notably, you may want to dismiss the controller from the main view controller. Don't forget to set the delegate property of the next view controller when you create it.

我希望这能解决您的问题!

I hope this clarifies your problem!

这篇关于尝试显示从一个 viewController 捕获的 UIImage,并在 iOS 中的另一个视图中显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆