ios - 核心图像 - 在 CMSampleBufferRef 上渲染透明图像导致其周围出现黑框

标签 ios avfoundation core-image cmsamplebufferref

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

当然,不再需要全局 EAGLContextCIContext

关于ios - 核心图像 - 在 CMSampleBufferRef 上渲染透明图像导致其周围出现黑框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24576243/

相关文章:

objective-c - 崩溃线程是iOS崩溃报告中唯一重要的线程吗?

ios - 控制 avqueueplayer 中项目的预缓冲

swift - AVCaptureDevicePosition.front 不工作

ios - 涉及 alpha 时 CISourceOverCompositing 的意外结果

AppStore 中应用程序的 iOS 共享扩展?

ios - 在 Objective-C 中解析 HTML

objective-c - 使用 avaudioplayer、UITableView NSURL 加载和播放音频

ios - 带有核心图像过滤器的 EAGLContext

swift - 无法使用 AVFoundation 将 JPEG 图像写入 HDD

ios - 在 iOS 中,如何让弹出窗口自行关闭?