iOS:警告:在演示文稿正在进行时,尝试在<ViewController>上显示<ModalViewController>

ala*_*usi 14 objective-c ios

我试图从图像选择器中选择一个图像后创建一个模态视图控制器.我用的代码是:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

但是,我收到警告:

Warning: Attempt to present <ModalViewController: 0x7561600> on <ViewController: 0x75a72e0> while a presentation is in progress!
Run Code Online (Sandbox Code Playgroud)

并且不显示模态视图.

我究竟做错了什么?

Mox*_*oxy 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];
                             }];
}
Run Code Online (Sandbox Code Playgroud)


Mid*_* MP 5

这里的问题正在发生,因为你首先解雇了,UIImagePicker并立即将另一个视图显示为模态视图.这就是你得到这个错误的原因.

请检查以下代码:

- (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];
  }];
}
Run Code Online (Sandbox Code Playgroud)

  • @Moxy:不喜欢这样.我们的目标是帮助他找到解决方案.有时两个人的答案是一样的.因为那是解决方案.我们需要解决他的问题,他的答案被接受并不重要. (2认同)