2012-11-30 74 views
14

我試圖在從圖像選取器中選取圖像後創建模式視圖控制器。我使用的代碼:iOS:警告:試圖在演示文稿正在進行時在<ViewController>上呈現<ModalViewController>

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

和模態視圖不出現。

我在做什麼錯?

+0

**完整的解決方案HERE **,約翰的回答是:http://stackoverflow.com/questions/14453001/meaning-of-warning-while-演示進行中 – Fattie

回答

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

只需添加,即在視圖控制器關閉後執行完成塊時,它不會立即執行。 viewWillAppear和viewDidAppear有機會在完成塊之前運行。 –

+0

一個簡短的說明,因爲我曾經被Runtastic的一名職員代理問過,我不知道: – NicTesla

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]; 
    }]; 
} 
+0

他仍然可以通過動畫解除它 – Moxy

+1

您的答案與我的答案一樣(我將刪除我的答案,因爲它與發佈兩次的答案無關)。解決方案是完成處理程序的方法。但是如果他使用了[self dismissModalViewControllerAnimated:NO],他根本就不會收到這個警告。 – Moxy

+0

@Moxy:不要刪除你的答案。兩個答案有很多不同之處。你的是最好的。所以不要刪除它。 –

相關問題