iphone - 如何为 AVVideoComposition 设置透明背景?

标签 iphone avfoundation transparent background-color avcomposition

更新 - 我为回答此问题添加了 50 点声誉赏金!

我有一个应用程序需要将一些视频和照片放在一起以创建电影。我正在使用 AVMutableComposition 来实现这一点。为了能够指示它如何合成视频,我必须使用 AVMutableVideoComposition。这个东西有一个名为 backgroundColor 的属性,Apple 文档说:

Only solid BGRA colors are supported; patterns and other supported colors are ignored. If the rendered pixel buffer does not have alpha, the alpha value of the background color is ignored.

我从中了解到的是,有一种方法可以将 alpha channel 添加到背景颜色中,但我只是不明白如何操作。 如果渲染的像素缓冲区没有 Alpha,则忽略背景颜色的 Alpha 值。 是什么意思?我怎样才能添加这样的东西?

如果我只是这样做 myVideoCompositionInstruction.backgroundColor = [[UIColor colorWithWhite:1 alpha:0] CGColor]; 它就不起作用 - 背景保持白色并且不会变得透明。

任何帮助将不胜感激,谢谢!

最佳答案

在 Apple 的 AVVideoCompositionInstruction 文档中,您要设置的属性 backgroundColor 的类型为 CGColorRef

我敢打赌,因为您将 backgroundColorUIColor 设置,然后才通过 CGColor 属性获取它的值,对于某些这是 alpha 值未传递到 CGColorRef 对象的原因。您知道白色值会被传递,因为默认颜色是黑色。

UIColor 有一些限制。我建议您阅读以下article 。请特别注意本文的“UIColor 问题” 部分。

我猜测解决您问题的最佳解决方案是直接使用 CGColor 定义 backgroundColor (例如使用 CGColorCreate ),而不是 UIColor。

关于iphone - 如何为 AVVideoComposition 设置透明背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6678743/

相关文章:

iphone - 重复使用通配符 appId 来启用推送通知

iphone - AVPlayer Streaming - 在特定时间播放流

iphone - 适用于雪豹的 Xcode 4.3

ios - AVPlayer 不在 iOS 11 中播放

ios - 播放 mp3/wav 声音导致异常

ios - CMSampleBufferRef 的深拷贝

java - 模型实例贴花之间的 Libgdx 剔除

ios - 如何从 iOS 中的谷歌地图返回 native 应用程序

html - 翻转图像下方有不需要的灰色条

android - 如何在我的 Activity 中设置状态(通知)栏的样式