iOS CAlayer Orientation AVCaptureVideoPreviewLayer doesn't rotate

后端 未结 8 733
既然无缘
既然无缘 2020-12-24 09:50

\"enter Summary: I can\'t force the CALayer to respond correctly to orientati

8条回答
  •  -上瘾入骨i
    2020-12-24 10:30

    This the method I use in the view controller to maintain the orientation of the capture layer so that it is always right-side-up:

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
        if (_previewLayer.connection.supportsVideoOrientation)
        {
        switch (toInterfaceOrientation)
            {
            case UIInterfaceOrientationPortrait:
                {
                _previewLayer.connection.videoOrientation = AVCaptureVideoOrientationPortrait;
                break;
                }
            case UIInterfaceOrientationPortraitUpsideDown:
                {
                _previewLayer.connection.videoOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
                break;
                }
            case UIInterfaceOrientationLandscapeLeft:
                {
                _previewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
                break;
                }
            case UIInterfaceOrientationLandscapeRight:
                {
                _previewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
                break;
                }
            }
        }
    

    A little wordy, but safe even if either enumeration ever changes in the future.

提交回复
热议问题