簡體   English   中英

在iOS上顯示CVImageBufferRef的最有效方法是什么

[英]What is the most efficient way to display CVImageBufferRef on iOS

我有CMSampleBufferRef(s),我使用VTDecompressionSessionDecodeFrame進行解碼,這在完成幀解碼后會導致CVImageBufferRef,所以我的問題是。

在UIView中顯示這些CVImageBufferRefs的最有效方法是什么?

我已經成功地將CVImageBufferRef轉換為CGImageRef並通過將CGImageRef設置為CALayer的內容進行顯示,但是DecompressionSession已通過@ {(id)kCVPixelBufferPixelFormatTypeKey:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA]進行了配置。

這是示例/代碼,我如何將CVImageBufferRef轉換為CGImageRef(注意:cvpixelbuffer數據必須為32BGRA格式才能正常工作)

    CVPixelBufferLockBaseAddress(cvImageBuffer,0);
    // get image properties 
    uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(cvImageBuffer);
    size_t bytesPerRow   = CVPixelBufferGetBytesPerRow(cvImageBuffer);
    size_t width         = CVPixelBufferGetWidth(cvImageBuffer);
    size_t height        = CVPixelBufferGetHeight(cvImageBuffer);

    /*Create a CGImageRef from the CVImageBufferRef*/
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef    cgContext  = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    CGImageRef cgImage = CGBitmapContextCreateImage(cgContext);

    // release context and colorspace 
    CGContextRelease(cgContext);
    CGColorSpaceRelease(colorSpace);

    // now CGImageRef can be displayed either by setting CALayer content
    // or by creating a [UIImage withCGImage:geImage] that can be displayed on
    // UIImageView ...

#WWDC14會話513( https://developer.apple.com/videos/wwdc/2014/#513 )暗示可以避免YUV-> RGB色彩空間轉換(使用CPU?),並且如果使用了YUV功能的GLES magic-想知道這可能是什么以及如何實現?

Apple的iOS SampleCode GLCameraRipple顯示了一個示例,該示例顯示了使用2個OpenGLES從相機捕獲的YUV CVPixelBufferRef,其中兩個Y和UV分量具有單獨的紋理,而一個片段着色器程序使用GPU進行YUV到RGB色彩空間的轉換計算-真正需要的全部就是有一些更直接的方法可以做到這一點嗎?

注意:在我的用例中,由於無法使用解壓縮的輸入,因此我無法使用AVSampleBufferDisplayLayer。

如果從CMSampleBufferRef獲取CVImageBufferRef ,而您是從captureOutput:didOutputSampleBuffer:fromConnection:接收的,則無需進行轉換,可以直接從CMSampleBufferRef獲取imageData。 這是代碼:

NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:sampleBuffer];
UIImage *frameImage = [UIImage imageWithData:imageData];

API說明不提供有關是否支持其32BGRA的任何信息,並在不應用任何壓縮的情況下以jpeg格式生成imageData以及任何元數據。 如果您的目標是在屏幕上顯示圖像或與UIImageView使用,這是一種快速的方法。

更新:以下原始答案不起作用,因為kCVPixelBufferIOSurfaceCoreAnimationCompatibilityKey對於iOS不可用。


UIViewCALayer支持,其contents屬性支持多種類型的圖像。 正如在針對macOS的類似問題的答案中所詳細描述的那樣,可以使用CALayer渲染CVPixelBuffer的支持IOSurface (注意:我僅在macOS上對此進行了測試。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM