问题描述
我有一个带有纯绿色背景的视频,我正在尝试使用 GPUImageChromaKeyFilter 使其透明.
I have a video with a solid green background, that I am trying to make transparent with GPUImageChromaKeyFilter.
当我为播放器视图设置清晰的颜色时,结果是视频不是真正透明,而是为背景着色:
When I have clear colour for the player view, the result is that the video is not really transparent, but tints the background:
当我把背景改成白色的时候,绿色的背景不见了,但是很明显视图是不透明的:
When I change the background to white, the green background is gone, but obviously the view is not transparent:
我做错了什么?
我的代码是:
let movie = GPUImageMovie(URL: url)
let filter = GPUImageChromaKeyFilter()
filter.setColorToReplaceRed(0, green: 1, blue: 0)
movie.addTarget(filter)
let playerView = GPUImageView()
playerView.backgroundColor = UIColor.clearColor()
playerView.setBackgroundColorRed(0, green: 0, blue: 0, alpha: 0)
let trinity = (movieFile, filter, playerView)
filter.addTarget(playerView)
movie.startProcessing()
我正在存储电影、过滤器和视图以避免 ARC 在退出范围时释放它们,我将视图添加到正确的超级视图并使用自动布局对其进行定位.
I am storing the movie, filter and view to avoid ARC releasing them when scope is exited and I add the view to the correct superview and position it with auto layout.
我使用的视频是 h264 视频 - https://dl.dropboxusercontent.com/u/1400954/Coin.mp4
The video I am using is an h264 video - https://dl.dropboxusercontent.com/u/1400954/Coin.mp4
推荐答案
我成功了.解决方案是使用 GPUImageChromaKeyBlendFilter.它通过用第二个源中的像素替换第一个源中 setColorToReplaceRed 中指定的颜色的像素来混合两个源.
I got this working. The solution is to use GPUImageChromaKeyBlendFilter.It blends two sources by replacing the pixels of the colour specified in setColorToReplaceRed in the first source with the pixels in the second source.
所以我用透明的 UIImage 制作了一个 GPUImagePicture 并将过滤器作为目标添加到它.您可以使用透明的 PNG 或构建一个 UIImage 并在代码中用透明填充它(空的 UIImage 不起作用!)
So I made a GPUImagePicture with a transparent UIImage and added the filter as a target to it. You can use a transparent PNG or build an UIImage and fill it with transparent in code (empty UIImage won't work!)
现在一切正常!
代码是
let filter = GPUImageChromaKeyBlendFilter()
filter.setColorToReplaceRed(0, green: 1, blue: 0)
let movieFile = GPUImageMovie(URL: url)
movieFile.addTarget(filter)
let backgroundImage = UIImage(named: "MTransparent")
let sourcePicture = GPUImagePicture(image: backgroundImage, smoothlyScaleOutput: true)
sourcePicture.addTarget(filter)
sourcePicture.processImage()
let playerView = GPUImageView()
playerView.backgroundColor = UIColor.clearColor()
filter.addTarget(playerView)
movieFile.startProcessing()
这篇关于使用 GPUImageChromaKeyFilter 的视频在透明 GPUImageView 中播放时有色调吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!