AVPreviewLayer 内容重力未填充图层
AVPreviewLayer contentsGravity not filling Layer
我正在使用 AVFoundation 进行一些视频录制,并且我一直在寻找如何使视频达到纵横比填充。我已经阅读了 apple 的 avfoundation 指南和 class AVCaptureVideoPreviewLayer 的参考资料。我也在这里 AVFoundation camera preview layer not working 阅读了这个问题。这是我的代码
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)
我把它放在 viewDidLayoutSubviews()
中,这样我就可以获得 captureView.frame 的正确大小,也就是我的 previewLayer 所在的 UIView
。知道为什么 aspectFill 不起作用吗?正如您从红色背景中看到的那样,图层大小正确,但 contentsGravity 未填充图层。
在 link AVCaptureVideoPreviewLayer 中找到了我的答案。我需要使用 videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
而不是 videoPreviewLayer.contentsGravity = AVLayerVideoGravityResizeAspectFill
对我来说,NSGangster 在问题中提供了我的问题的答案:
I put this in viewDidLayoutSubviews() so that I can get the correct size for captureView.frame which is the UIView my previewLayer is inside of.
我最初在 viewDidLoad()
中有代码,这意味着在确定视图的真实边界之前图层正在调整大小。
感谢分享。
我正在使用 AVFoundation 进行一些视频录制,并且我一直在寻找如何使视频达到纵横比填充。我已经阅读了 apple 的 avfoundation 指南和 class AVCaptureVideoPreviewLayer 的参考资料。我也在这里 AVFoundation camera preview layer not working 阅读了这个问题。这是我的代码
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)
我把它放在 viewDidLayoutSubviews()
中,这样我就可以获得 captureView.frame 的正确大小,也就是我的 previewLayer 所在的 UIView
。知道为什么 aspectFill 不起作用吗?正如您从红色背景中看到的那样,图层大小正确,但 contentsGravity 未填充图层。
在 link AVCaptureVideoPreviewLayer 中找到了我的答案。我需要使用 videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
而不是 videoPreviewLayer.contentsGravity = AVLayerVideoGravityResizeAspectFill
对我来说,NSGangster 在问题中提供了我的问题的答案:
I put this in viewDidLayoutSubviews() so that I can get the correct size for captureView.frame which is the UIView my previewLayer is inside of.
我最初在 viewDidLoad()
中有代码,这意味着在确定视图的真实边界之前图层正在调整大小。
感谢分享。