我正在尝试在使用 AVFoundation 的 AVCaptureVideoDataOutput 录制的视频上添加水印/ Logo 。 我的类设置为 sampleBufferDelegate 并接收 CMSamplebufferRefs。我已经对 CMSampleBufferRefs CVPixelBuffer 应用了一些效果并将其传递回 AVAssetWriter。
左上角的 Logo 使用透明 PNG 格式提供。 我遇到的问题是 UIImage 的透明部分在写入视频后变成黑色。 任何人都知道我做错了什么或可能忘记了什么?
下面的代码片段:
//somewhere in the init of the class;
_eaglContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
_ciContext = [CIContext contextWithEAGLContext:_eaglContext
options: @{ kCIContextWorkingColorSpace : [NSNull null] }];
//samplebufferdelegate method:
- (void) captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection {
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
....
UIImage *logoImage = [UIImage imageNamed:@"logo.png"];
CIImage *renderImage = [[CIImage alloc] initWithCGImage:logoImage.CGImage];
CGColorSpaceRef cSpace = CGColorSpaceCreateDeviceRGB();
[_ciContext render:renderImage
toCVPixelBuffer:pixelBuffer
bounds: [renderImage extent]
colorSpace:cSpace];
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
CGColorSpaceRelease(cSpace);
....
}
看起来 CIContext 没有绘制 CIImages alpha。 有什么想法吗?
最佳答案
对于遇到同样问题的开发者:
似乎在 GPU 上渲染并写入视频的任何内容最终都会在视频中形成一个黑洞。 相反,我删除了上面的代码,创建了一个 CGContextRef,就像您在编辑图像时所做的那样,并利用该上下文。
代码:
....
CVPixelBufferLockBaseAddress( pixelBuffer, 0 );
CGContextRef context = CGBitmapContextCreate(CVPixelBufferGetBaseAddress(pixelBuffer),
CVPixelBufferGetWidth(pixelBuffer),
CVPixelBufferGetHeight(pixelBuffer),
8,
CVPixelBufferGetBytesPerRow(pixelBuffer),
CGColorSpaceCreateDeviceRGB(),
(CGBitmapInfo)
kCGBitmapByteOrder32Little |
kCGImageAlphaPremultipliedFirst);
CGRect renderBounds = ...
CGContextDrawImage(context, renderBounds, [overlayImage CGImage]);
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
CGColorSpaceRelease(cSpace);
....
当然,不再需要全局 EAGLContext
和 CIContext
。
关于ios - 核心图像 - 在 CMSampleBufferRef 上渲染透明图像导致其周围出现黑框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24576243/