CIContext渲染:toCVPixelBuffer:bounds:colorSpace:函数不适用于带alpha通道的图像

时间:2017-11-30 14:47:20

标签: ios objective-c core-image cmsamplebuffer

我正在尝试使用AVFoundation的AVCaptureVideoDataOutput在我正在录制的视频上添加水印/徽标。我遇到的问题是,一旦写入视频,UIImage的透明部分就是黑色。我是什么我做错了吗?

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);

1 个答案:

答案 0 :(得分:1)

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

    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);