2016-07-26 165 views
3

我想从CMSampleBufferRef中检索一个CVPixelBufferRef,以便改变CVPixelBufferRef来动态覆盖水印。CMSampleBufferGetImageBuffer返回null

我正在使用CMSampleBufferGetImageBuffer(sampleBuffer)来实现此目的。我打印返回的CVPixelBufferRef的结果,但它始终为空。

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection { 

    CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 

    NSLog(@"PixelBuffer %@",pixelBuffer); 
... 

} 

我有什么我失踪?

回答

4

经过数小时的调试后,结果表明样本可能是视频或音频样本。所以试图从音频缓冲区获取CVPixelBufferRef返回null。

我在继续之前通过检查样本类型来解决它。由于我对音频样本不感兴趣,我只需返回音频样本。

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection { 

    CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 

    CMFormatDescriptionRef formatDesc = CMSampleBufferGetFormatDescription(sampleBuffer); 
    CMMediaType mediaType = CMFormatDescriptionGetMediaType(formatDesc); 

    //Checking sample type before proceeding 
    if (mediaType == kCMMediaType_Audio) 
    {return;} 

//Processing the sample... 

}