根据设备方向旋转视频预览图层。 Xcode Swift

时间:2018-05-08 22:18:39

标签: ios swift xcode avfoundation orientation

我一直试图让我的实时视频输入(相机预览图层)在旋转设备时旋转方向。我完全卡住了。我在下面提供了我当前的代码。

注意:此应用是iMessage扩展程序而不是普通应用程序。

我目前的代码:

 func setupPreviewLayer() {
        cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspect
        orientationChange()
        cameraPreviewLayer?.frame.size.height = self.view.frame.size.height / 2
        cameraPreviewLayer?.frame.size.width = self.view.frame.size.width / 2
        liveVideoFeedPosition()
        cameraPreviewLayer?.position.x = self.view.frame.width / 2
        cameraPreviewLayer?.position.y = self.view.frame.height / 4  //- 33 original
        self.view.layer.addSublayer(cameraPreviewLayer!)
    }

**不连续

 func liveVideoFeedPosition() {
        if presentationStyle == MSMessagesAppPresentationStyle.compact {
            holoLogo.isHidden = true
            cameraPreviewLayer?.position.x = self.view.frame.width / 2
            cameraPreviewLayer?.position.y = self.view.frame.height / 4  //+33 original
        }
        if presentationStyle == MSMessagesAppPresentationStyle.expanded {
            holoLogo.isHidden = false
            cameraPreviewLayer?.position.x = self.view.frame.width / 2
            cameraPreviewLayer?.position.y = self.view.frame.height / 2  //- 27 original
        }
        self.OutlineImage.center.y = self.view.center.y // - 27 original
        self.OutlineImage.center.x = self.view.center.x
        self.view.addSubview(OutlineImage)
    }

**不连续

 override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {

        if UIDevice.current.orientation == .portrait {
            captureSession.stopRunning()
            self.view.layer.sublayers?.removeAll()
            orientation = "Portrait"
            setupInputOutput()
            setupPreviewLayer()
            captureSession.startRunning()
        }
        if UIDevice.current.orientation == .portraitUpsideDown {
            captureSession.stopRunning()
            self.view.layer.sublayers?.removeAll()
            orientation = "Portrait UpsideDown"
            setupInputOutput()
            setupPreviewLayer()
            captureSession.startRunning()
        }

        if UIDevice.current.orientation == .landscapeLeft {
            captureSession.stopRunning()
            self.view.layer.sublayers?.removeAll()
            orientation = "Landscape Left"
            setupInputOutput()
            setupPreviewLayer()
            captureSession.startRunning()
        }
        if UIDevice.current.orientation == .landscapeRight {
            captureSession.stopRunning()
            self.view.layer.sublayers?.removeAll()
            orientation = "Landscape Right"
            setupInputOutput()
            setupPreviewLayer()
            captureSession.startRunning()
        }
        liveVideoFeedPosition()
    }

**不连续

var orientation = "Portrait"

    func orientationChange() {
        if orientation == "Portrait" {
            cameraPreviewLayer?.connection?.videoOrientation = .portrait
        }
        if orientation == "Portrait UpsideDown" {
            cameraPreviewLayer?.connection?.videoOrientation = .portraitUpsideDown
        }
        if orientation == "Landscape Right" {
            cameraPreviewLayer?.connection?.videoOrientation = .landscapeRight
        }
        if orientation == "Landscape Left" {
            cameraPreviewLayer?.connection?.videoOrientation = .landscapeLeft
        }
    }

所有这些代码都不是特定的顺序。

0 个答案:

没有答案