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

1

4

I'm trying to add a watermark/logo on a video that I'm recording using AVFoundation's AVCaptureVideoDataOutput.The problem I'm having is that the transparent parts of the UIImage are black once written to the video.What I'm doing wrong ?

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);
Kliber answered 30/11, 2017 at 14:47 Comment(0)
H
5

You can composite the image which will preserve transparency and render that to the pixel buffer. For example:

    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);
Hinman answered 30/11, 2017 at 23:55 Comment(1)
Hi beyowulf Thanks for answer, it work for me. But now i have other problem, i am rendering the transparent logo to every CVPixelBufferRef in "AVCaptureVideoDataOutputSampleBufferDelegate -(void) captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection" function and after "end capture" i have audio problem. It dose not coincides with video.Kliber

© 2022 - 2024 — McMap. All rights reserved.