14

I am trying to create a modal view controller after picking an image from the image picker. I use the code :

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

   NSLog(@"Picker has returned");
   [self dismissModalViewControllerAnimated:YES];



   // TODO: make this all threaded?
   // crop the image to the bounds provided
   img = [info objectForKey:UIImagePickerControllerOriginalImage];
   NSLog(@"orig image size: %@", [[NSValue valueWithCGSize:img.size] description]);

   // save the image, only if it's a newly taken image:
   if([picker sourceType] == UIImagePickerControllerSourceTypeCamera){
      UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
   }

   // self.image_View.image=img;
   //self.image_View.contentMode = UIViewContentModeScaleAspectFit;


   ModalViewController *sampleView = [[ModalViewController alloc] init];
   [self presentModalViewController:sampleView animated:YES];
}

However, I receive the warning:

Warning: Attempt to present <ModalViewController: 0x7561600> on <ViewController: 0x75a72e0> while a presentation is in progress!

and the modal view does not appear.

What am I doing wrong?

Yuliani Noriega
  • 1,085
  • 12
  • 23
alandalusi
  • 1,145
  • 4
  • 18
  • 39
  • **COMPLETE SOLUTION HERE**, John's answer: http://stackoverflow.com/questions/14453001/meaning-of-warning-while-a-presentation-is-in-progress – Fattie Jan 24 '14 at 07:44

2 Answers2

28
- (void)imagePickerController:(UIImagePickerController *)picker 
didFinishPickingMediaWithInfo:(NSDictionary *)info {

     // TODO: make this all threaded?
     // crop the image to the bounds provided
     img = [info objectForKey:UIImagePickerControllerOriginalImage];
     NSLog(@"orig image size: %@", [[NSValue valueWithCGSize:img.size] description]);

     // save the image, only if it's a newly taken image:
     if ([picker sourceType] == UIImagePickerControllerSourceTypeCamera) {
         UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
     }

     // self.image_View.image = img;
     // self.image_View.contentMode = UIViewContentModeScaleAspectFit;

    NSLog(@"Picker has returned");
    [self dismissViewControllerAnimated:YES
                             completion:^{
                                ModalViewController *sampleView = [[ModalViewController alloc] init];
                                [self presentModalViewController:sampleView animated:YES];
                             }];
}
rizjoj
  • 197
  • 2
  • 9
Moxy
  • 4,162
  • 2
  • 30
  • 49
  • 1
    Just to add that while the completion block executes after the view controller is dismissed, it is not immediately after. viewWillAppear and viewDidAppear get a chance to run before your completion block. – Dan Loughney Apr 30 '14 at 15:13
  • One short note, because i was once asked by a staff agent at Runtastic and i didn't know that: – NicTesla Oct 20 '14 at 09:21
5

Here the issue is happening because, you are first dismissing the UIImagePicker and immediately you are displaying another view as modal view. That's why you are getting this error.

Check with the following code:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

 [self dismissViewControllerAnimated:NO completion:^{

     img = [info objectForKey:UIImagePickerControllerOriginalImage];
     NSLog(@"orig image size: %@", [[NSValue valueWithCGSize:img.size] description]);

     if([picker sourceType] == UIImagePickerControllerSourceTypeCamera)
     {
         UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
     }

     ModalViewController *sampleView = [[ModalViewController alloc] init];
    [self presentModalViewController:sampleView animated:YES];
  }];
}
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • He still can dismiss it with animation – Moxy Nov 30 '12 at 09:36
  • 1
    Your answer is just like mine (I'm gonna delete mine as it's irrelevant to have the same answer posted twice) . The solution is the method with the completion handler. But if he had used [self dismissModalViewControllerAnimated:NO] he wouldn't receive that warning at all. – Moxy Nov 30 '12 at 10:00
  • @Moxy: don't delete your answer dude. There is lot of difference between both answers. Your's is best. So don't delete it. – Midhun MP Nov 30 '12 at 10:03
  • I don't want it to seem that I stole the answer from you. I was editing it before you posted yours. Anyway my point is the solution is the same as @alandalusi should be using -dismissViewControllerAnimated:completion: to dismiss the modal view controller also because -dismissModalViewControllerAnimated: has been deprecated since iOS 6. – Moxy Nov 30 '12 at 10:08
  • 2
    @Moxy: don't feel like so. Our aim is to help him to find a solution. Sometimes two people's answer will be same. Because that is the solution. We need to solve his issue, whose answer is accepted that won't matter. – Midhun MP Nov 30 '12 at 10:15