iOS CAlayer Orientation AVCaptureVideoPreviewLayer doesn't rotate

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

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

8条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-24 10:39

    The answer using willRotateToUserInterfaceOrientation works fine, except that that method has been deprecated. So if you're able to use iOS 9, then here's the way to do it, in Swift:

    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
            let newOrientation = UIDevice.currentDevice().orientation
            switch newOrientation {
            case .LandscapeLeft:
                self.capturePreviewLayer?.connection.videoOrientation = .LandscapeLeft
            case .LandscapeRight:
                self.capturePreviewLayer?.connection.videoOrientation = .LandscapeRight
            case .Portrait, .Unknown, .FaceUp, .FaceDown:
                self.capturePreviewLayer?.connection.videoOrientation = .Portrait
            case .PortraitUpsideDown:
                self.capturePreviewLayer?.connection.videoOrientation = .PortraitUpsideDown
            }
            super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
        }
    

提交回复
热议问题