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);
M. Arman
  • 43
  • 5

1 Answers1

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);
beyowulf
  • 15,101
  • 2
  • 34
  • 40
  • 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. – M. Arman Dec 04 '17 at 22:21