Trying to display a UIImage captured from one viewController, and display it in another in iOS

戏子无情 提交于 2019-12-13 22:21:15

问题


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];

}

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.


回答1:


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.

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!




回答2:


Firstly, you should create a property in secondViewController to pass your mainViewController to before present the secondViewController. Ex:

_secondViewController.mainViewController = self;

Secondly, you will need a property in mainViewController to pass the image back from secondViewController. Ex:

self.mainViewController.capturedImage = _myImage;

Hopefully this will help you.



来源:https://stackoverflow.com/questions/18470017/trying-to-display-a-uiimage-captured-from-one-viewcontroller-and-display-it-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!