2017-04-27 4 views
8

私は、ビューコントローラのビュー階層に追加AVCaptureVideoPreviewLayerインスタンスを持っています。AVCaptureVideoPreviewLayer誤った向きUI回転が無効になっている

-viewDidLayoutSubviewsメソッドでレイヤーフレームが更新されます。ビューコントローラの向きがUIInterfaceOrientationMaskLandscapeRightにロックされています。

問題は、次の通りである:

  1. 装置が横向き
  2. ビューコントローラがモーダル提示さに保持される - 正しくビデオレイヤーディスプレイ。 Correct orientation
  3. デバイスは、その後、ロックされ、それがロックされている間、デバイスが縦向きに回転されます。
  4. 依然として縦方向に、数秒間であるビデオ層は90度回転表示されている間、デバイスは、次にロック解除されます。ただし、ビデオレイヤのフレームは正しいです。他のすべてのUI要素は正しく表示されます。数秒後、レイヤーが正しい向きにスナップします。 Incorrect orientation

下の層とUI要素の境界を見つけてください、私は(結果なしで)次のようにビデオ層の向きを更新する試みた:

  • AVCaptureSessionDidStartRunningNotificationUIApplicationDidBecomeActiveNotification通知に加入
  • -viewWillTransitionToSize:withTransitionCoordinator:メソッドが呼び出されたときに更新を呼び出す
  • -viewWillAppear:

問題は、ビデオ層の向き自体に接続されていないようですが、より多くの階層のレイアウトを表示します。

UPDATE:

示唆したように、私も助けていないデバイスの向きの変更のビデオ層の向きを更新してみました。

私はまた、アプリケーションが起動され、画面が最初に提示された後、問題が主に起こることに気づきました。同じセッション中、後続の画面プレゼンテーションでは、問題の再生率が(1/20のようなもの)は本当に低いです。

+1

登録enterForeground通知のために、FUNCのfixOrientationを追加()メソッドでは、viewWillAppear上とenterForegroundにそれを呼び出す: UIDevice.current.setValue(のNSNumber(値:UIInterfaceOrientation。私はこのコードを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

関連する問題