核心图像 – 在CMSampleBufferRef上渲染一个透明的图像会导致它周围的黑框
我正在使用AVFoundation的AVCaptureVideoDataOutput录制的video上添加水印/徽标。 我的类被设置为sampleBufferDelegate并接收CMSamplebufferRefs。 我已经将某些效果应用于CMSampleBufferRefs CVPixelBuffer,并将其传回给AVAssetWriter。
左上angular的徽标使用透明PNG提供。 我遇到的问题是,一旦写入video,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阿尔法。 有任何想法吗?
对于遇到同样问题的开发人员:
它看起来在GPU上呈现的任何东西,并写入video结束了video中的黑洞。 相反,我删除了上面的代码,创build了一个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
不再需要了。
- Progressive Video在iOS上下载
- 模拟AVLayerVideoGravityResizeAspectFill:裁剪和中心video模仿预览,而不会失去清晰度
- AVAssetExportSession – 在IOS中join2个mp4文件
- 在swift 2中无法使用AVCaptureVideoDataOutput通过CGDataProviderCopyData获取像素数据
- 如何使用swift监视ios上的audioinput – 示例?
- iPhone 7 Plus – AVFoundation双摄像头
- iOS 10.0 – 10.1:使用AVVideoCompositionCoreAnimationTool后,AVPlayerLayer不显示video,只有audio
- 我怎样才能从iPhone相机快速处理图像?
- 如何loggingvideo并保持音乐在后台播放?