2

我正在尝试在使用 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);
4

1 回答 1

3

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

    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);
于 2017-11-30T23:55:50.667 回答