我正在尝试使用简单的渲染摄像机输出到金属层管道,并且它在Objective-C(有MetalVideoCapture示例应用程序)中运行良好,但似乎有一些格式怪异的时候我试着把它翻译成快速的。我的超级捕获缓冲区看起来像这样(忽略缺乏消毒......)
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)'
问题是,如果我用nil替换outTexture它会停止抛出错误,但显然这对我没有帮助。根据函数的参考我需要UnsafeMutablePointer?>为了最后一个价值。我不知道该怎么做。
答案 0 :(得分:3)
尝试事先分配你的textureCache,这是我用作成员变量的东西:
var _videoTextureCache : Unmanaged<CVMetalTextureCacheRef>?
然后我通过
在初始化方法中分配textureCacheCVMetalTextureCacheCreate(kCFAllocatorDefault, nil, _context.device, nil, &_videoTextureCache)
其中_context.device是MTLDevice。然后,在captureOutput方法中,我使用以下内容(注意,此处不包含错误检查)
var textureRef : Unmanaged<CVMetalTextureRef>?
CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault, _videoTextureCache!.takeUnretainedValue(), imageBuffer, nil, MTLPixelFormat.BGRA8Unorm, width, height, 0, &textureRef)
我希望这有帮助!
答案 1 :(得分:2)
更新@ peacer212对Swift 3的回答。
您不再需要Unmanaged
和takeUnretainedValue
。所以代码应该是:
var textureCache: CVMetalTextureCache?
...
CVMetalTextureCacheCreate(kCFAllocatorDefault, nil, _context.device, nil, &_videoTextureCache)
...
var textureRef : CVMetalTexture?
CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault, textureCache!, imageBuffer, nil, .bgra8Unorm, width, height, 0, & textureRef)