Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用CVMetalTextureCacheCheCreateTextureFromImage在Swift中将CMSampleBuffer转换为CVMetalTexture_Swift_Avfoundation_Metal - Fatal编程技术网

使用CVMetalTextureCacheCheCreateTextureFromImage在Swift中将CMSampleBuffer转换为CVMetalTexture

使用CVMetalTextureCacheCheCreateTextureFromImage在Swift中将CMSampleBuffer转换为CVMetalTexture,swift,avfoundation,metal,Swift,Avfoundation,Metal,我正在尝试将一个简单的渲染摄影机输出到金属层管道,它在Objective-C中工作得很好(有MetalVideoCapture示例应用程序),但当我尝试将其转换为swift时,似乎有一些格式上的奇怪之处。我的ultrasimple捕获缓冲区如下所示(忽略缺少消毒…) 其中videoTextureCache是var videoTextureCache:CVMetalTextureCache?=无 但是它让我无法使用类型为“(CFAllocator!、CVMetalTextureCache、CVI

我正在尝试将一个简单的渲染摄影机输出到金属层管道,它在Objective-C中工作得很好(有MetalVideoCapture示例应用程序),但当我尝试将其转换为swift时,似乎有一些格式上的奇怪之处。我的ultrasimple捕获缓冲区如下所示(忽略缺少消毒…)

其中videoTextureCache是
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)