2012-12-17 16 views
0

當我嘗試關閉我的UIImagePickerController時,它崩潰了應用程序。錯誤是:「終止應用程序由於未捕獲的異常'UIApplicationInvalidInterfaceOrientation',原因:'preferredInterfaceOrientationForPresentation必須返回支持的接口方向!'」關閉相機視圖崩潰應用程序

我有我的視圖控制器中設置的首選接口方向。

-(NSUInteger)supportedInterfaceOrientations 
{ 
return UIInterfaceOrientationMaskPortrait; 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
return UIInterfaceOrientationPortrait; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
return UIInterfaceOrientationIsPortrait(interfaceOrientation); 
} 

- (BOOL) shouldAutorotate { 
return YES; 
} 

這裏是我打電話,調出攝像頭的方法,這工作正常添加的攝像頭,但就像我說的,崩潰當我嘗試取出相機。

-(IBAction)addCamera:(id)sender 
{ 

self.cameraController = [[UIImagePickerController alloc] init]; 
self.cameraController.sourceType = UIImagePickerControllerSourceTypeCamera; 

self.cameraController.cameraViewTransform = CGAffineTransformScale(self.cameraController.cameraViewTransform, 
                    1.13f, 
                    1.13f); 

self.cameraController.showsCameraControls = NO; 
self.cameraController.navigationBarHidden = YES; 

self.wantsFullScreenLayout = YES; 

ar_overlayView = [[UIView alloc] initWithFrame:CGRectZero]; 

self.view = ar_overlayView; 
[self.cameraController setCameraOverlayView:ar_overlayView]; 
[self presentViewController:cameraController animated:NO completion:nil]; 
[ar_overlayView setFrame:self.cameraController.view.bounds]; 
} 

-(IBAction)back:(id)sender 
{ 
[ar_overlayView removeFromSuperview]; 
[cameraController dismissViewControllerAnimated:NO completion:nil]; 
} 
+0

如果我嘗試使用視圖控制器以關閉照相機,如在另外崩潰 - (IBAction爲)背面:(ID)發送方 { [ar_overlayView removeFromSuperview]; [self dismissViewControllerAnimated:NO completion:nil]; } –

+0

還有一個問題:在哪裏以及如何聲明'ar_overlayView'? – gregheo

+0

它是在我的@implementation後面聲明的,不知道那是什麼,但看起來像這樣。 (AT)接口ARViewController() (AT)端 (AT)實施ARViewController 的UIView * ar_overlayView; UIView * button_overlayView; (at)合成..... –

回答

1

好了,找到了解決辦法,這是很簡單的,我只是改變了我的背法到:

[self dismissModalViewControllerAnimated:YES]; 

現在我的相機消失了,我可以看到我的原始視圖,當我按下後退按鈕。 我仍然沒有弄清楚是什麼導致原始問題,因爲我已經通過了info.plist和支持方向的方法,但是這完成了我想要的。

我仍然好奇是什麼導致了錯誤,但如果任何人有任何想法。

+0

如果你支持的只是肖像,那麼'shouldAutorotate'應該返回'NO'。 –

+0

我試過將它們全部設置爲no,並沒有解決問題。 –

0

您無法從其超級視圖中移除UIViewController的主視圖。

取而代之的是:

self.view = ar_overlayView; 

試試這個:

[self.view addSubview:ar_overlayView]; 

然後,你就可以從上海華正確地將其刪除。

+0

謝謝,但仍然崩潰。 –

+0

[cameraController dismissViewControllerAnimated:NO completion:nil]; - 這是我的程序崩潰的代碼行,如果我發表評論,它不會崩潰。 –

+0

嘗試'[self.cameraController dismissViewControllerAnimated:NO completion:nil];'而不是'[cameraController dismissViewControllerAnimated:NO completion:nil];' – colincameron

0

您應該使用類似於下面的didFinishPickingMedieWithInfo方法並使用[self dismissModalViewControllerAnimated:YES];

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

{ 

if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) { 

    NSLog(@"Camera"); 


} 

else { 


    NSLog(@"Album"); 

} 

[self dismissModalViewControllerAnimated:YES]; 
} 
+0

該應用程序仍然崩潰。也許我應該將圖像選取器控制器移到它自己的視圖控制器文件中,然後用應用程序委託調用它,而不是試圖從主視圖控制器中運行它? –

+0

@StevenMarlowe在應用程序崩潰時應該無關緊要,因爲在沒有相機覆蓋的情況下關閉相機時?我有一種感覺,這可能是問題... –

+0

並且您是否在.h中添加了uinavigationcontroller委託方法?像這樣@interface TakePicViewController:UIViewController

0

我不認爲你需要自己添加ar_overlayView到當前視圖,如果它是一個攝像頭覆蓋。

這裏是你的代碼現在正在做的:

  1. 添加ar_overlayView當前視圖
  2. 添加ar_overlayView與相機的重疊視圖
  3. 顯示攝像機視圖(模式)

在這一點上,ar_overlayView正在顯示兩次。當您在關閉相機視圖時發送removeFromSuperview消息時,它可能會感到困惑,因爲它同時處於兩個視圖層次結構中。

跳過self.view = ar_overlayView;[self.view addSubview:ar_overlayView];行應該可以解決問題。

而且,dismissViewControllerAnimated:completion:應該發送到同一個對象presentViewController:animated:completion是(在這種情況下self)呼籲:

-(IBAction)addCamera:(id)sender 
{ 
    // -- snip -- // 
    ar_overlayView = [[UIView alloc] initWithFrame:self.cameraController.view.bounds]; 
    [self.cameraController setCameraOverlayView:ar_overlayView]; 
    [self presentViewController:self.cameraController animated:NO completion:nil]; 
} 

-(IBAction)back:(id)sender 
{ 
    [self dismissViewControllerAnimated:NO completion:nil]; 
} 
+0

仍然沒有工作,我認爲答案在主視圖控制器的某處,但不知道該怎麼做,因爲我已經設置了首選方向的方法。 –

1

您可以嘗試刪除此方法:

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 

這將修復它。但是有時它會導致減少stateBar高度。