Objective c AVCaptureVideoPreviewLayer方向错误

Objective c AVCaptureVideoPreviewLayer方向错误,objective-c,xcode,cocoa,Objective C,Xcode,Cocoa,我有个奇怪的问题。我正在我的应用程序中录制视频。在我将设备旋转180度之前,一切正常。无论我在哪种横向模式下启动游戏,视频都会正确启动,但一旦我从横向向左旋转到右侧,视频就会开始垂直录制。我打了个平局,以表明: 我正在尝试这样设置视频方向: -(BOOL)应该自动旋转{ UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation]; if (orientation == UIInterfaceOrient

我有个奇怪的问题。我正在我的应用程序中录制视频。在我将设备旋转180度之前,一切正常。无论我在哪种横向模式下启动游戏,视频都会正确启动,但一旦我从横向向左旋转到右侧,视频就会开始垂直录制。我打了个平局,以表明:

我正在尝试这样设置视频方向: -(BOOL)应该自动旋转{

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIInterfaceOrientationLandscapeLeft) {
    NSLog(@"Left");
    captureVideoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
    return YES;
}
else if(orientation == UIInterfaceOrientationLandscapeRight) {
    NSLog(@"Right");
   captureVideoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
    return YES;
}

return NO;
} 但这似乎根本不起作用。我设置了captureVideoPreviewLayer.frame=self.view.bounds;
还是不行

你所做的是正确的,只是我不确定你的方向。我想应该是这样

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
以下是您可以通过调用
shouldAutorotate
方法来尝试的完整方法:

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

statusBarOrientation
从iOS 9.0开始就不推荐使用。