2017-04-27 32 views
8

我有一個AVCaptureVideoPreviewLayer實例被添加到視圖控制器視圖層次結構中。當UI旋轉被禁用時,AVCaptureVideoPreviewLayer方向錯誤

- (void) loadView { 
    ... 

    self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:nil]; 
    self.previewLayer.frame = self.view.bounds; 
    self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 

    [self.view.layer addSublayer: _previewLayer]; 

    // adding other UI elements 
    ... 
} 

... 

- (void) _setupPreviewLayerWithSession: (AVCaptureSession*) captureSession 
{ 
    self.previewLayer.session = self.captureManager.captureSession; 
    self.previewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight; 
} 

層幀在-viewDidLayoutSubviews方法更新。視圖控制器方向鎖定爲UIInterfaceOrientationMaskLandscapeRight

的問題是如下:

  1. 設備在橫向
  2. 視圖控制器被模態呈現保持 - 視頻層顯示正確。 Correct orientation
  3. 設備被鎖定,鎖定時設備旋轉至縱向。
  4. 該設備然後解鎖而仍然在縱向和幾秒鐘的視頻層顯示旋轉90度。但是,視頻圖層的幀是正確的。所有其他UI元素都能正確顯示。幾秒鐘之後,圖層捕捉到正確的方向。 請找到該層和UI元素界限以下 Incorrect orientation

我已經嘗試更新視頻層方向如下(無結果):

  • 訂閱AVCaptureSessionDidStartRunningNotificationUIApplicationDidBecomeActiveNotification通知
  • -viewWillTransitionToSize:withTransitionCoordinator:方法調用時調用更新
  • on -viewWillAppear:

該問題似乎並未與視頻層定位本身相關聯,但更多的是查看層次結構佈局。

UPDATE:

至於建議,我也嘗試更新設備上的方向變化,這並沒有幫助視頻層取向。

我也注意到這個問題大多發生在應用程序啓動並且屏幕首次出現之後。在同一會話期間的後續屏幕演示中,問題的再現率非常低(類似1/20)。

+1

註冊enterForeground通知,添加FUNC fixOrientation()調用它viewWillAppear中和enterForeground,在方法: UIDevice.current.setValue(NSNumber的(價值:UIInterfaceOrientation。如果它的landscapeLeft/Right,我幾年前用Obj-c編寫這樣的代碼,所以寫作評論,因爲我還沒有測試 – SeanLintern88

回答

3

試試這個代碼:

[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(orientationChanged:) 
               name:UIDeviceOrientationDidChangeNotification 
               object:nil]; 
-(void)orientationChanged:(NSNotification *)notif { 

    [_videoPreviewLayer setFrame:_viewPreview.layer.bounds]; 
    if (_videoPreviewLayer.connection.supportsVideoOrientation) { 
     _videoPreviewLayer.connection.videoOrientation = [self interfaceOrientationToVideoOrientation:[UIApplication sharedApplication].statusBarOrientation]; 
    } 

} 

- (AVCaptureVideoOrientation)interfaceOrientationToVideoOrientation:(UIInterfaceOrientation)orientation { 
    switch (orientation) { 
     case UIInterfaceOrientationPortrait: 
      return AVCaptureVideoOrientationPortrait; 
     case UIInterfaceOrientationPortraitUpsideDown: 
      return AVCaptureVideoOrientationPortraitUpsideDown; 
     case UIInterfaceOrientationLandscapeLeft: 
      return AVCaptureVideoOrientationLandscapeLeft; 
     case UIInterfaceOrientationLandscapeRight: 
      return AVCaptureVideoOrientationLandscapeRight; 
     default: 
      break; 
    } 
// NSLog(@"Warning - Didn't recognise interface orientation (%d)",orientation); 
    return AVCaptureVideoOrientationPortrait; 
} 
+0

謝謝,我嘗試過實施你的建議,那也沒有幫助。請參閱問題中的更新部分了解更多信息。 – NikGreen

相關問題