2014-01-23 55 views
2

我有一個iPhone相機應用程序,我在其中使用AVCaptureVideoPreviewLayer。當某人拍攝照片/ stillImage時,這會顯示在新的ViewController中。一旦這個視圖控制器被解僱,魔法就會發生。AVCaptureVideoPreviewLayer,視頻方向卡住

整個應用程序正常旋轉,兩個viewcontroller;但有一個例外。如果我在一個方向上拍攝圖片,請在新的ViewController中旋轉設備,然後返回到AVCaptureVideoPreviewLayer,除了圖像以外,整個圖層都沿着正確的方向旋轉,因此突然間,輸入會橫向顯示在previewLayer中。

我已經檢查過,並嘗試設置PreviewLayer的幀,但這一切似乎都很好,我在調試時看到的值都是正確的。這只是顯示的圖像偏斜。來回旋轉設備可以在使用過程中解決這個問題。

有沒有人見過這個之前,有沒有人有線索如何解決這個問題?

+0

這是iPhone還是iPad?適用於帶縱向模式的iphone手柄。如果iPhone視圖控制器允許旋轉,我已經看到AVCapture發生了一些愚蠢的事情。 –

+0

@SamBudda它是iPhone的,正如我所說的。除了這種情況之外,它是完美的。 – ophychius

回答

2

由於didRotateFromInterfaceOrientation:在iOS的8棄用當界面旋轉

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 
    self.previewLayer.connection.videoOrientation = self.interfaceOrientation; 
} 
+0

不幸的是,這是行不通的,因爲viewController不是當時的活動的,因此這個函數沒有被調用。然而,這確實使我指向了正確的方向。解決方案是編寫自己的函數,將其從活動視圖中傳遞給方向,並在其頂部的活動ViewController改變方向時通過通知調用它。 – ophychius

0

我以前有過類似的問題。 一旦駁回相機控制器,你可以刷新視圖旋轉與此:

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 
UIWindow *window = [[UIApplication sharedApplication] keyWindow]; 
UIView *topView = [window.subviews objectAtIndex:0]; 
[topView removeFromSuperview]; 
[window addSubview:topView];   

因此,除去頂部顯示窗口上大多數觀點和重置它的窗口,它應該正確刷新視圖旋轉。

希望它可以幫助

+0

不幸的是,這似乎並沒有解決我的問題。謝謝你的努力。 – ophychius

0

也許,不是改變AVCaptureVideoPreviewLayer框架,你可以嘗試把它放在一個容器UIView,並將此容器的框架。 (我修正了'VCaptureVideoPreviewLayer this way : sizing directly PreviewLayer had no effect, but sizing its containing UIView`工作的'尺寸'錯誤)。

或者 也許你可以強制定向您的viewController viewWillAppear(或viewDidAppear?)

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    // try and force current orientation 
    UIApplication *application = [UIApplication sharedApplication]; 
    UIInterfaceOrientation currentOrientation = application.statusBarOrientation; 
    [application setStatusBarOrientation:currentOrientation animated:animated]; 
} 

祝你好運!

1

你可以改變previewLayer的連接的videoOrientation,我改變連接的視頻取向viewDidLayoutSubviews

switch ([UIApplication sharedApplication].statusBarOrientation) { 
    case UIInterfaceOrientationPortrait: 
     self.captureVideoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationPortrait; 
     break; 
    case UIInterfaceOrientationPortraitUpsideDown: 
     self.captureVideoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationPortraitUpsideDown; 
     break; 
    case UIInterfaceOrientationLandscapeLeft: 
     self.captureVideoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeLeft; 
     break; 
    case UIInterfaceOrientationLandscapeRight: 
     self.captureVideoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight; 
     break; 
    default: 
     break; 
} 
0

由於interfaceOrientation已棄用,最新的swift 2.0解決方案應爲:

let previewLayerConnection = self.previewLayer.connection 
let orientation = AVCaptureVideoOrientation(rawValue: UIApplication.sharedApplication().statusBarOrientation.rawValue) 
previewLayerConnection.videoOrientation = orientation!