3

我已经尝试了所有关闭 UIImagePickerController 的变体,但没有任何运气。我究竟做错了什么。

- (IBAction)choosePhoto
{
    self.picker = [[UIImagePickerController alloc] init];
    self.picker.delegate = self;
    self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentModalViewController:self.picker animated:YES];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)imagePicker
{
    NSLog(@"dismiss image picker");
    [self dismissModalViewControllerAnimated:NO];
    [[self.picker parentViewController] dismissModalViewControllerAnimated:NO];
    [self.presentedViewController dismissModalViewControllerAnimated:NO];
    [self.presentingViewController dismissModalViewControllerAnimated:NO];
     // And every other way i could think of
}

- (void)imagePickerController:(UIImagePickerController *)imagePicker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    .. same stuff here
}

我试图从父母、祖父母、navigationController 和根控制器中展示选择器,但没有任何效果。无论我做什么,我都不能解雇 ImagePickerController。

请注意,每次都会调用日志语句。

干杯

4

4 回答 4

9

试试这条线。它可能对你有用。

[self.picker dismissModalViewControllerAnimated:NO];

对于iOS 6及更高版本,请使用此

[self.picker dismissViewControllerAnimated:NO completion:nil];

也使用此代码来展示您的选择器控制器

if ([self respondsToSelector:@selector(presentViewController:animated:completion:)]){
    [self presentViewController:self.picker animated:YES completion:nil];
} else {
    //To target iOS 5.0
    [self presentModalViewController:self.picker animated:YES];
}
于 2013-05-10T05:58:24.250 回答
6

你在运行 iOS 6 吗?如果presentModalViewController:是这样,则已弃用,可能会导致一些意外结果。尝试presentViewController:animated:completion:改用。

但从技术上讲,这是您应该做的所有事情:

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)imagePicker
{
   [imagePicker dismissViewControllerAnimated:NO completion:nil];//Or call YES if you want the nice dismissal animation
}
于 2013-05-10T05:46:42.960 回答
6

对于 Swift 使用这个:

func imagePickerControllerDidCancel(picker: UIImagePickerController!) {
    picker.dismissViewControllerAnimated(true, completion: nil)
}
于 2015-01-12T13:06:59.227 回答
3

对于斯威夫特 4:

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
}
于 2018-01-29T14:31:14.693 回答