3

I have tried every variation of dismissing a UIImagePickerController with out any luck. What am i doing wrong.

- (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
}

I have tried to present the picker from the parent, grandparent, navigationController and root controller and nothing works. What ever i do i cant dismiss the ImagePickerController.

Please note the log statement gets called every time.

Cheers

user7388
  • 1,741
  • 2
  • 19
  • 25
user346443
  • 4,672
  • 15
  • 57
  • 80

4 Answers4

9

Try this line. It might work for you.

[self.picker dismissModalViewControllerAnimated:NO];

And for iOS 6 and later use this

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

Also use this code to present your picker controller

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];
}
Divyu
  • 1,325
  • 9
  • 21
6

Are you running iOS 6? If so, presentModalViewController: is deprecated and could be causing some unexpected results. Try using presentViewController:animated:completion: instead.

But technically, here's all you should have to do:

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)imagePicker
{
   [imagePicker dismissViewControllerAnimated:NO completion:nil];//Or call YES if you want the nice dismissal animation
}
Tommy Devoy
  • 13,441
  • 3
  • 48
  • 75
6

For Swift use this:

func imagePickerControllerDidCancel(picker: UIImagePickerController!) {
    picker.dismissViewControllerAnimated(true, completion: nil)
}
Vukašin Manojlović
  • 3,717
  • 3
  • 19
  • 31
Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130
3

For Swift 4:

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
}
Gefilte Fish
  • 1,600
  • 1
  • 18
  • 17