菜鸟教程小白 发表于 2022-12-11 18:51:55

ios - 将 MTLTexture 转换为 CVPixelBuffer


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

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

<p><code>context?.render(drawImage, to: targetTexture, commandBuffer: commandBuffer, bounds: targetRect, colorSpace: colorSpace)
</code></p>

<p>它可以正确渲染,因为我可以看到 MetalView 上显示的图像。</p>

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

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

<p>任何帮助将不胜感激,谢谢!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以尝试像这样从 MTLTexture 复制原始数据:</p>

<pre><code>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, &amp;outPixelbuffer);
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 将 MTLTexture 转换为 CVPixelBuffer,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/46513889/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/46513889/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 将 MTLTexture 转换为 CVPixelBuffer