iOS:警告:在演示文稿正在进行时尝试在<viewcontroller>上显示<modalviewcontroller> </viewcontroller> </modalviewcontroller>

时间:2012-11-30 09:26:35

标签: 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];
}

但是,我收到警告:

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

并且不会出现模态视图。

我做错了什么?

2 个答案:

答案 0 :(得分: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];
                             }];
}

答案 1 :(得分: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];
  }];
}