使用CVMetalTextureCacheCheCreateTextureFromImage在Swift中将CMSampleBuffer转换为CVMetalTexture
我正在尝试将一个简单的渲染摄影机输出到金属层管道,它在Objective-C中工作得很好(有MetalVideoCapture示例应用程序),但当我尝试将其转换为swift时,似乎有一些格式上的奇怪之处。我的ultrasimple捕获缓冲区如下所示(忽略缺少消毒…) 其中videoTextureCache是使用CVMetalTextureCacheCheCreateTextureFromImage在Swift中将CMSampleBuffer转换为CVMetalTexture,swift,avfoundation,metal,Swift,Avfoundation,Metal,我正在尝试将一个简单的渲染摄影机输出到金属层管道,它在Objective-C中工作得很好(有MetalVideoCapture示例应用程序),但当我尝试将其转换为swift时,似乎有一些格式上的奇怪之处。我的ultrasimple捕获缓冲区如下所示(忽略缺少消毒…) 其中videoTextureCache是var videoTextureCache:CVMetalTextureCache?=无 但是它让我无法使用类型为“(CFAllocator!、CVMetalTextureCache、CVI
var videoTextureCache:CVMetalTextureCache?=无
但是它让我无法使用类型为“(CFAllocator!、CVMetalTextureCache、CVImageBuffer、nil、MTLPixelFormat、Int、Int、inout-CVMetalTextureRef)”的参数列表调用“CVMetalTextureCacheCreateTextureFromImage”
问题是,如果我用nil替换outTexture,它将停止抛出错误,但显然这对我没有帮助。根据函数的引用,最后一个值需要UnsafeMutablePointer?>。我不知道如何获取。请尝试提前分配textureCache,以下是我用作成员变量的内容:
var _videoTextureCache : Unmanaged<CVMetalTextureCacheRef>?
其中_context.device是MTL设备。然后,在captureOutput方法中,我使用以下内容(注意,此处不包括错误检查)
var-textureRef:非托管?
CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault、\u videoTextureCache!.takeUnrepainedValue()、imageBuffer、nil、MTLPixelFormat.BGRA8Unorm、宽度、高度、0和textureRef)
我希望这有帮助 更新@peacer212对Swift 3的回答 您不再需要
非托管
和获取未恢复值
。因此,代码应该是:
var textureCache: CVMetalTextureCache?
...
CVMetalTextureCacheCreate(kCFAllocatorDefault, nil, _context.device, nil, &_videoTextureCache)
...
var textureRef : CVMetalTexture?
CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault, textureCache!, imageBuffer, nil, .bgra8Unorm, width, height, 0, & textureRef)
明亮的我也一直在努力解决这个问题——非常感谢:)这个答案已经过时了。查看@XueYu对Swift 3和Swift 4的回答。什么是videoTextureCache?
CVMetalTextureCacheCreate(kCFAllocatorDefault, nil, _context.device, nil, &_videoTextureCache)
var textureRef : Unmanaged<CVMetalTextureRef>?
CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault, _videoTextureCache!.takeUnretainedValue(), imageBuffer, nil, MTLPixelFormat.BGRA8Unorm, width, height, 0, &textureRef)
var textureCache: CVMetalTextureCache?
...
CVMetalTextureCacheCreate(kCFAllocatorDefault, nil, _context.device, nil, &_videoTextureCache)
...
var textureRef : CVMetalTexture?
CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault, textureCache!, imageBuffer, nil, .bgra8Unorm, width, height, 0, & textureRef)