AVCaptureVideoOrientation无法正常工作

时间:2016-03-26 19:16:48

标签: ios swift avcapturesession

我知道这个问题已遍布堆栈溢出但我使用的每个答案都不会影响结果。我的问题是视频方向总是在横向左侧,我被告知是默认的videoOrientation。我希望它在用户持有iPhone的任何方向(主要是肖像),这是我用来做的代码:

        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        self.cameraView.layer.addSublayer(previewLayer!)

        // Tell the preview layer the orientation to display
        self.previewLayer?.frame = self.view.bounds

        if let connection =  self.previewLayer?.connection  {
            let currentDevice: UIDevice = UIDevice.currentDevice()
            let orientation: UIDeviceOrientation = currentDevice.orientation

            let previewLayerConnection : AVCaptureConnection = connection

            if (previewLayerConnection.supportsVideoOrientation)
            {
                switch (orientation)
                {
                case .Portrait:
                    previewLayerConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
                    break
                case .LandscapeRight:
                    previewLayerConnection.videoOrientation = AVCaptureVideoOrientation.LandscapeLeft
                    break
                case .LandscapeLeft:
                    previewLayerConnection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight
                    break
                case .PortraitUpsideDown:
                    previewLayerConnection.videoOrientation = AVCaptureVideoOrientation.PortraitUpsideDown
                    break


                default:
                    previewLayerConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
                    break
                }
            }
        }

        captureSession.startRunning()
    }

由于某种原因,这不起作用我仍然最终得到了景观中的所有东西。我认为问题出在previewLayer上,我觉得我应该把它分配给以下的东西:

previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)

因为否则它应该可以工作但是我不知道该怎么做请帮助我,我已经在这几个小时了!

0 个答案:

没有答案