OGeek|极客世界-中国程序员成长平台

标题: ios - 将 MTLTexture 转换为 CVPixelBuffer [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:51
标题: ios - 将 MTLTexture 转换为 CVPixelBuffer

我目前正在使用 Metal 开发实时过滤器。 定义我的 CIImage 后,我将图像渲染为 MTLTexture。

下面是我的渲染代码。 context 是一个由 Metal 支持的 CIContext; targetTexture 是附加到我的 MTKView 实例的 currentDrawable 属性的纹理的别名:

context?.render(drawImage, to: targetTexture, commandBuffer: commandBuffer, bounds: targetRect, colorSpace: colorSpace)

它可以正确渲染,因为我可以看到 Metal View 上显示的图像。

问题是在渲染图像(并显示它)之后,我想提取 CVPixelBuffer 并使用 AVAssetWriter 类将其保存到磁盘。

另一种选择是有两个渲染步骤,一个渲染到纹理,另一个渲染到 CVPixelBuffer。 (但不清楚如何创建这样的缓冲区,或者两个渲染步骤对帧率的影响)

任何帮助将不胜感激,谢谢!



Best Answer-推荐答案


您可以尝试像这样从 MTLTexture 复制原始数据:

var outPixelbuffer: CVPixelBuffer?
if let datas = targetTexture.texture.buffer?.contents() {
    CVPixelBufferCreateWithBytes(kCFAllocatorDefault, targetTexture.width, 
    targetTexture.height, kCVPixelFormatType_64RGBAHalf, datas, 
    targetTexture.texture.bufferBytesPerRow, nil, nil, nil, &outPixelbuffer);
}

关于ios - 将 MTLTexture 转换为 CVPixelBuffer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46513889/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4