AVPreviewLayer的内容重力未填充图层

NSG*_*ter 1 avfoundation ios swift

在此处输入图片说明

我正在使用AVFoundation进行一些视频录制,并且到处都在寻找如何将视频进行纵横比填充。我已经阅读了苹果的AVFoundation指南和AVCaptureVideoPreviewLayer的类Referene。我在这里还阅读了此问题,AVFoundation相机预览层无法正常工作。这是我的代码

        videoPreviewLayer.frame = captureView.frame
        videoPreviewLayer.frame.origin = CGPoint(x: 0, y: 0)
        videoPreviewLayer.backgroundColor = UIColor.redColor().CGColor
        videoPreviewLayer.contentsGravity = AVLayerVideoGravityResizeAspectFill
        videoPreviewLayer.masksToBounds = true
        captureView.layer.addSublayer(videoPreviewLayer)
Run Code Online (Sandbox Code Playgroud)

我把它放进去,viewDidLayoutSubviews()以便可以为captureView.frame获得正确的大小,这是UIView我的PreviewLayer所在的大小。任何提示为何AspectFill无法正常工作?从红色背景可以看出,该图层的大小正确,但是contentsGravity并未填充该图层。

NSG*_*ter 6

在此链接AVCaptureVideoPreviewLayer中找到了我的答案。我需要用videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill代替videoPreviewLayer.contentsGravity = AVLayerVideoGravityResizeAspectFill