我試圖獲得一個簡單的渲染相機輸出到金屬層管道,它在Objective-C(有MetalVideoCapture示例應用程序)中工作得很好,但當我嘗試將其翻譯爲swift時,似乎會出現一些格式不正常的情況。我ultrasimple捕獲緩衝看起來像這樣(忽略缺乏消毒的...)使用CVMetalTextureCacheCreateTextureFromImage在Swift中將CMSampleBuffer轉換爲CVMetalTexture
func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
var error: CVReturn! = nil
let sourceImageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
let width = CVPixelBufferGetWidth(sourceImageBuffer!)
let height = CVPixelBufferGetHeight(sourceImageBuffer!)
var outTexture: CVMetalTextureRef? = nil
error = CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault, videoTextureCache!, sourceImageBuffer!, nil, MTLPixelFormat.BGRA8Unorm, width, height, 0, &outTexture!)
if error != nil {
print("Error! \(error)")
}
let videoTexture = CVMetalTextureGetTexture(outTexture!)
self.imageTexture = videoTexture!
}
哪裏videoTextureCache是var videoTextureCache: CVMetalTextureCache? = nil
但它給了我Cannot invoke 'CVMetalTextureCacheCreateTextureFromImage' with an argument list of type '(CFAllocator!, CVMetalTextureCache, CVImageBuffer, nil, MTLPixelFormat, Int, Int, Int, inout CVMetalTextureRef)'
的事情是,如果我替換outTexture零它停止拋出錯誤,但顯然這不會幫助我。根據函數的參考,我需要UnsafeMutablePointer?>作爲最後一個值。我不知道如何得到。
太棒了!我也一直在摔跤 - 這要感謝一萬美元:) –
嗨,你有沒有更新Swift 3? – jperl