如何在Swift中检测并应用横向方向到AVCaptureVideoPreviewLayer?

epa*_*alm 5 landscape orientation swift ios8

我基本上是在回答这个问题AVCaptureVideoPreviewLayer方向 -除了在Swift中需要景观.

我的目标是iOS 8,而AVCaptureVideoPreviewLayer似乎没有setVideoOrientation功能.

我应该如何检测方向已更改,并正确旋转AVCaptureVideoPreviewLayer?

小智 19

检测并应用它viewWillLayoutSubviews.

这是如何做:

override func viewWillLayoutSubviews() {

    let orientation: UIDeviceOrientation = UIDevice.currentDevice().orientation
    print(orientation)

    switch (orientation) {
    case .Portrait:
        previewLayer?.connection.videoOrientation = .Portrait
    case .LandscapeRight:
        previewLayer?.connection.videoOrientation = .LandscapeLeft
    case .LandscapeLeft:
        previewLayer?.connection.videoOrientation = .LandscapeRight
    default:
        previewLayer?.connection.videoOrientation = .Portrait
    }
}
Run Code Online (Sandbox Code Playgroud)