了解如何解雇UIImagePickerController

时间:2013-11-10 23:47:16

标签: ios objective-c uiimagepickercontroller

我有点困惑,这不是关于如何解雇UIImagePickerController的问题,它更像是一个“为什么那样工作”类型的问题。我在iOS7工作。

在线查看苹果文档(此链接:https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/CameraAndPhotoLib_TopicsForIOS/Articles/TakingPicturesAndMovies.html#//apple_ref/doc/uid/TP40010406-SW6),我发现以下代码解雇了UIImagePickerController:

[[picker parentViewController] dismissModalViewControllerAnimated: YES];

编辑:我知道该方法已被弃用,因此我尝试了以下操作,但这也不起作用:

[[picker parentViewController] dismissViewControllerAnimated:YES completion:NULL];

那些对我不起作用。然而,这确实有效:

[picker dismissViewControllerAnimated:YES completion:NULL];
  • Apple文档错了吗?还是刚刚过时?
  • 为什么第二位代码有效,因为根据我的理解,它的父视图控制器工作可以解除类似于弹出窗口的东西,或者在这种情况下是UIImagePickerController

谢谢。

编辑:这是我呈现UIImagePickerController的方式,UITapGestureRecognizer调用第一个方法然后调用第二个方法。

- (IBAction)captureMoment:(UITapGestureRecognizer *)sender {
    [self startCameraCaptureFromViewController:self usingDelegate:self];
}

- (BOOL)startCameraCaptureFromViewController:(UIViewController *)controller
                               usingDelegate:(id <UIImagePickerControllerDelegate, UINavigationControllerDelegate>)delegate {

    if (delegate == nil || controller == nil || [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO) {
        return NO;
    }

    UIImagePickerController *camera = [[UIImagePickerController alloc] init];
    camera.sourceType = UIImagePickerControllerSourceTypeCamera;

    camera.allowsEditing = NO;

    camera.delegate = delegate;

    [controller presentViewController:camera animated:YES completion:NULL];

    return YES;
}

2 个答案:

答案 0 :(得分:4)

有两种方法对我有用

  1. 使用picker.presentingViewController代替picker.parentViewController,由rmaddy
  2. 回答
  3. imagePickerControllerDidCancel:[picker dismissViewControllerAnimated:YES completion:NULL];
  4. 中使用以下代码

    以下是完整的代码,它放在呈现UIImagePickerController的UIViewController中:

    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
        [picker.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
    }
    

答案 1 :(得分:0)

自iOS6以来,方法dismissModalViewControllerAnimated已被弃用。您使用的新方法是正确的。