CIContext 渲染:toCVPixelBuffer:边界:colorSpace:函数不适用于具有 alpha 通道的图像

CIContext render: toCVPixelBuffer: bounds: colorSpace: function does not work for images with alpha channel

我正在尝试在我正在使用 AVFoundation 录制的视频上添加 watermark/logo 我遇到的问题是 UIImage 的透明部分在写入后变为黑色video.What 我做错了吗?

CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(buffer);

.....

.....

CIImage *image = [[CIImage alloc] initWithData:logoData]; 
CVPixelBufferLockBaseAddress( pixelBuffer, 0 );

CIContext *ciContext = [CIContext contextWithOptions:nil];

CGColorSpaceRef cSpace = CGColorSpaceCreateDeviceRGB();

[ciContext render:image toCVPixelBuffer:pixelBuffer bounds:CGRectMake(image.extent.origin.x, image.extent.origin.y - 2, image.extent.size.width, image.extent.size.height) colorSpace:cSpace];


CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
CGColorSpaceRelease(cSpace);

您可以合成将保留透明度的图像并将其渲染到像素缓冲区。例如:

    CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CIImage *cameraImage = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer];
    CGColorSpaceRef cSpace = CGColorSpaceCreateDeviceRGB();
    cameraImage = [self.logoImage imageByCompositingOverImage:cameraImage];
    [self.context render:cameraImage toCVPixelBuffer:pixelBuffer bounds:cameraImage.extent colorSpace:cSpace];

    CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
    CGColorSpaceRelease(cSpace);