Trying to display a UIImage captured from one view

2019-09-01 15:47发布

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.

2条回答
贼婆χ
2楼-- · 2019-09-01 16:00

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!

查看更多
迷人小祖宗
3楼-- · 2019-09-01 16:24

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.

查看更多
登录 后发表回答