2016-07-26 162 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... 

}