How to set a transparent background for an AVVideoComposition?

帅比萌擦擦* 提交于 2019-12-21 05:14:14

问题


Update - I've added a 50 reputation bounty for answering this question!

I have an application that needs to put together some videos and photos to create a movie out of them. I am using AVMutableComposition for that. To be able to instruct it how the videos get composed I have to use an AVMutableVideoComposition. This thing has a property called backgroundColor and Apple Documentation says:

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.

What I understand from this is that there is a way to add alpha channel to the backgroundColor but I just don't understand how. What does If the rendered pixel buffer does not have alpha, the alpha value of the background color is ignored. mean? And how I can add such thing?

If I just do myVideoCompositionInstruction.backgroundColor = [[UIColor colorWithWhite:1 alpha:0] CGColor]; it just doesn't work - the background stays white and doesn't go transparent.

Any help would be much appreciated, Thanks!


回答1:


In Apple's documentation for AVVideoCompositionInstruction the property you want to set, backgroundColor, is of type CGColorRef.

My bet is, because you are setting backgroundColor with UIColor and only then getting the value of it through the CGColor property, for some reason the alpha value doesn't get passed into a CGColorRef object. You know the white value gets passed because the default color is black.

UIColor has some limitations. I suggest you read the following article. Take particular notice of the section "The problems with UIColor" of the article.

I'm guessing the best solution for your problem is defining backgroundColor with CGColor directly (use CGColorCreate, for instance), instead of UIColor.



来源:https://stackoverflow.com/questions/6678743/how-to-set-a-transparent-background-for-an-avvideocomposition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!