首页 > 解决方案 > Swift AVMutableVideoComposition 比例和中心计算

问题描述

我正在缩放 AVMutableVideoComposition 以适合我的全屏视图。

当我这样做时,它从左下角缩放,所以我试图将它居中。这是我的代码...

let scale = max(videoViewBounds.width/source.extent.size.width, videoViewBounds.height/source.extent.size.height)
        let screenScale = UIScreen.main.scale

        let translateX = (source.extent.size.width * scale) - videoViewBounds.width
        let translateY = (source.extent.size.height * scale) - videoViewBounds.height
        print("X: ", translateX)
        print("Y: ", translateY)

        let sourceTransform = CGAffineTransform.init(scaleX: scale * screenScale, y: scale * screenScale).translatedBy(x: -translateX/2, y: -translateY/2)

        overlayFilter.setValue(source.transformed(by: sourceTransform), forKey: kCIInputBackgroundImageKey)

这给了我一个非常接近我想要的结果,但它有点偏离。如果我改变

-translate/2 to -translate/1.5

这似乎是完美的。但我不明白为什么。

有任何想法吗?还是更好的缩放和居中方式?

谢谢

标签: swiftscalecentercropavmutablecomposition

解决方案


推荐阅读