【发布时间】:2015-08-17 00:32:17
【问题描述】:
我正在使用AVCaptureVideoDataOutput 和AVCaptureAudioDataOutput 录制视频和音频,在captureOutput:didOutputSampleBuffer:fromConnection: 委托方法中,我想将文本绘制到我从视频连接接收到的每个单独的样本缓冲区上。文本几乎每帧都会发生变化(它是秒表标签),我希望将其记录在捕获的视频数据之上。
以下是我目前所能想到的:
//1.
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
//2.
UIImage *textImage = [self createTextImage];
CIImage *maskImage = [CIImage imageWithCGImage:textImage.CGImage];
//3.
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSDictionary *options = [NSDictionary dictionaryWithObject:(__bridge id)colorSpace forKey:kCIImageColorSpace];
CIImage *inputImage = [CIImage imageWithCVPixelBuffer:pixelBuffer options:options];
//4.
CIFilter *filter = [CIFilter filterWithName:@"CIBlendWithMask"];
[filter setValue:inputImage forKey:@"inputImage"];
[filter setValue:maskImage forKey:@"inputMaskImage"];
CIImage *outputImage = [filter outputImage];
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
//5.
[self.renderContext render:outputImage toCVPixelBuffer:pixelBuffer bounds:[outputImage extent] colorSpace:CGColorSpaceCreateDeviceRGB()];
//6.
[self.pixelBufferAdaptor appendPixelBuffer:pixelBuffer withPresentationTime:timestamp];
- 我在这里抓取像素缓冲区,非常简单。
- 我使用核心图形将文本写入空白 UIImage(
createTextImage就是这样做的。我能够验证此步骤是否有效;我将带有文本的图像保存到我的照片中。 - 我从像素缓冲区创建了一个 CGImage。
- 我为
CIBlendWithMask制作了一个CIFilter,将输入图像设置为从原始像素缓冲区创建的图像,并将输入掩码设置为CIImage,该图像由带有文本的图像制成。 - 最后,我将过滤器输出图像渲染到像素缓冲区。
CIContext是使用[CIContext contextWithOptions:nil];预先创建的。 - 毕竟,我将像素缓冲区附加到我的
pixelBufferAdaptor并带有适当的时间戳。
在录制结束时保存的视频没有可见的变化,即没有将蒙版图像绘制到像素缓冲区。
有人知道我在哪里出错了吗?我已经坚持了好几天了,任何帮助将不胜感激。
编辑:
- (UIImage *)createTextImage {
UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height), NO, 1.0);
NSMutableAttributedString *timeStamp = [[NSMutableAttributedString alloc]initWithString:self.timeLabel.text attributes:@{NSForegroundColorAttributeName:self.timeLabel.textColor, NSFontAttributeName: self.timeLabel.font}];
NSMutableAttributedString *countDownString = [[NSMutableAttributedString alloc]initWithString:self.cDownLabel.text attributes:@{NSForegroundColorAttributeName:self.cDownLabel.textColor, NSFontAttributeName:self.cDownLabel.font}];
[timeStamp drawAtPoint:self.timeLabel.center];
[countDownString drawAtPoint:self.view.center];
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return blank;
}
【问题讨论】:
-
嗨!你的问题是对我问题的回答。但是 self.renderContext 是什么?
-
这是一个
CIContext对象 -
你最后是怎么做到的?
-
我最终使用 GPUImage 录制实时视频,然后对结果应用了过滤器。
-
你能分享那个示例代码吗? @barndog 我正在使用 GPUImage,但无法达到同样的效果。
标签: ios objective-c avfoundation core-image core-video