Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Warning: Attempt to present <ModalViewController> on <ViewController>while a presentation is in progress

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?

like image 546
alandalusi Avatar asked Nov 30 '12 09:11

alandalusi


2 Answers

- (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];
                             }];
}
like image 192
Moxy Avatar answered Nov 03 '22 17:11

Moxy


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];
  }];
}
like image 23
Midhun MP Avatar answered Nov 03 '22 19:11

Midhun MP