【问题标题】:AVPreviewLayer contentsGravity not filling LayerAVPreviewLayer 内容重力不填充图层
【发布时间】:2016-03-08 19:36:48
【问题描述】:

我正在使用 AVFoundation 进行一些视频录制,并且我已经查看了如何让视频进行方面填充。我已经阅读了苹果的 avfoundation 指南和 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 没有填充图层。

【问题讨论】:

    标签: ios swift avfoundation


    【解决方案1】:

    在此链接AVCaptureVideoPreviewLayer 中找到了我的答案。我需要使用videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill 而不是videoPreviewLayer.contentsGravity = AVLayerVideoGravityResizeAspectFill

    【讨论】:

      【解决方案2】:

      对我来说,我的问题的答案是由 NSGangster 在问题中提供的:

      我把它放在 viewDidLayoutSubviews() 中,这样我就可以获得 captureView.frame 的正确大小,这是我的 previewLayer 所在的 UIView。

      我最初在viewDidLoad() 中有代码,这意味着在确定视图的真实边界之前图层正在调整大小。

      感谢分享。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-12
        • 2016-05-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多