如何创建由 CVPixelBuffer 支持的 MTLTexture [英] How to create a MTLTexture backed by a CVPixelBuffer

查看:103
本文介绍了如何创建由 CVPixelBuffer 支持的 MTLTexture的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

生成由 CVPixelBuffer 支持的 MTLTexture 的正确方法是什么?

What's the correct way to generate a MTLTexture backed by a CVPixelBuffer?

我有以下代码,但它似乎泄漏:

I have the following code, but it seems to leak:

func PixelBufferToMTLTexture(pixelBuffer:CVPixelBuffer) -> MTLTexture
{
    var texture:MTLTexture!

    let width = CVPixelBufferGetWidth(pixelBuffer)
    let height = CVPixelBufferGetHeight(pixelBuffer)

    let format:MTLPixelFormat = .BGRA8Unorm


    var textureRef : Unmanaged<CVMetalTextureRef>?

    let status = CVMetalTextureCacheCreateTextureFromImage(nil,
                                                           videoTextureCache!.takeUnretainedValue(),
                                                           pixelBuffer,
                                                           nil,
                                                           format,
                                                           width,
                                                           height,
                                                           0,
                                                           &textureRef)

    if(status == kCVReturnSuccess)
    {
        texture = CVMetalTextureGetTexture(textureRef!.takeUnretainedValue())
    }

    return texture
}

推荐答案

啊,我错过了:textureRef?.release()

Ah, I was missing: textureRef?.release()

这篇关于如何创建由 CVPixelBuffer 支持的 MTLTexture的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆