AVCaptureVideoPreviewLayer在调整大小时创建2个视图

时间:2018-04-30 09:40:42

标签: ios swift view autoresizingmask

我已经在CameraViewController上附加了一个Camera对象。 Camera对象里面有以下方法,它在给定的视图控制器上设置AVCaptureVideoPreviewLayer

func displayPreview(on view: UIView) throws {
        guard let captureSession = self.captureSession, captureSession.isRunning else {
            throw CameraControllerError.captureSessionIsMissing
        }

        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        view.layer.insertSublayer(self.previewLayer!, at: 0)
        previewLayer?.frame = view.bounds
        previewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
        previewLayer?.connection?.videoOrientation = .portrait
        previewLayer?.masksToBounds = true
    }

在CameraViewController中,要设置相机对象,我正在执行以下操作

var camera = Camera()

override func viewDidLoad() {
        super.viewDidLoad()
        configureView()
    }


    override func viewWillLayoutSubviews() {
        try? self.camera.displayPreview(on: self.view)
    }


    private func configureView(){
        self.view.addTapGestureRecognizer(action: {
            if let parentView = self.parent?.view.frame{
                UIView.animate(withDuration: 0.4, animations: {
                    self.view.layer.cornerRadius = 0
                    self.view.layer.masksToBounds = true
                    self.view.layer.borderWidth = 0
                    self.view.frame = parentView
                }, completion: nil)
            }
        })
    } 

这一切都运作良好,以至于视图正在调整大小。当发生这种情况时,看起来相机正在创建2个视图,如图片中,顶部较旧的视图和更新的一个视图的大小。不仅如此,但是当视图调整大小时,看起来像previewLayer在调整大小之前重新加载(在更大的视图出现之前需要一点)。我想要实现的是连续动画相机预览调整大小视图。我需要在代码中更改什么?我一直在玩try? self.camera.displayPreview(on: self.view)放在不同的视图加载部分,但这并没有改变任何东西。

https://go.microsoft.com/fwlink/?LinkId=760847

0 个答案:

没有答案