CIContext render: toCVPixelBuffer: bounds: colorSpace: function不能用于带有alpha通道的图像。

4

我正在尝试使用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个回答

5

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

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

嗨beyowulf,谢谢你的回答,它对我有用。但现在我有另一个问题,我正在将透明标志渲染到“AVCaptureVideoDataOutputSampleBufferDelegate -(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection”函数中的每个CVPixelBufferRef中,在“结束捕获”后我遇到了音频问题。它与视频不一致。 - M. Arman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接