当我使用func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!)
时,我需要在sampleBuffer中添加一个透明图像,但我总是得到黑色背景,代码在这里:
let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
CVPixelBufferLockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags(rawValue: 0))
let eagleContext = EAGLContext(api: .openGLES3)
let ciContex = CIContext.init(eaglContext: eagleContext!, options: [kCIContextWorkingColorSpace:NSNull()])
let filterdImage = CIImage(cgImage: #imageLiteral(resourceName: "att_id").cgImage!)
let colorSpace = CGColorSpaceCreateDeviceRGB()
ciContex.render(filterdImage, to: pixelBuffer!, bounds: filterdImage.extent, colorSpace: colorSpace)
CVPixelBufferUnlockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags(rawValue: 0))
videoWriterInput.append(sampleBuffer)
我想知道如何获得背景清晰的颜色。
注意:此答案假设您有一个背景,您希望在其上渲染带有透明部件的图像。
我没有找到让 CIContext 删除其基本黑色背景的方法,所以我设法解决这个问题的唯一方法是创建两个图层:
- 一个"基础"层(对我来说,它只是一个
CIColor.white
,但它可以成为你的背景视频帧,任何东西,没有透明度( - 图像的图层
这意味着最终的上下文呈现将如下所示:
let base = CIImage(color: .white) // or some other CIImage which covers the whole frame.
let finalImage = imageWithTransparency.composited(over: base)
context.render(finalImage,
to: somePixelBuffer,
bounds: finalImage.extent,
colorSpace: someColorSpaceMatchingThePixelBuffer)