首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将MTLTexture转换为CVPixelBuffer

将MTLTexture转换为CVPixelBuffer
EN

Stack Overflow用户
提问于 2017-10-01 23:28:42
回答 2查看 3.3K关注 0票数 4

我目前正在研究使用Metal的动态滤镜。在定义了我的CIImage之后,我将图像呈现为一个MTLTexture。

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

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

它渲染正确,因为我可以看到在金属视图上显示的图像。

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

另一种选择是有两个渲染步骤,一个渲染到纹理,另一个渲染到CVPixelBuffer。(但尚不清楚如何创建这样的缓冲区,也不清楚两个渲染步骤在帧率中会产生什么影响)

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

EN

回答 2

Stack Overflow用户

发布于 2018-05-15 10:49:10

您可以尝试从MTLTexture复制原始数据,如下所示:

代码语言:javascript
复制
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);
}
票数 3
EN

Stack Overflow用户

发布于 2021-01-06 20:57:39

代码语言:javascript
复制
+ (void)getPixelBufferFromBGRAMTLTexture:(id<MTLTexture>)texture result:(void(^)(CVPixelBufferRef pixelBuffer))block
{

CVPixelBufferRef pxbuffer = NULL;
 NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
                          [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
                          nil];
 
size_t imageByteCount = texture.width * texture.height * 4;
void *imageBytes = malloc(imageByteCount);
NSUInteger bytesPerRow = texture.width * 4;
MTLRegion region = MTLRegionMake2D(0, 0, texture.width, texture.height);
[texture getBytes:imageBytes bytesPerRow:bytesPerRow fromRegion:region mipmapLevel:0];

CVPixelBufferCreateWithBytes(kCFAllocatorDefault,texture.width,texture.height,kCVPixelFormatType_32BGRA,imageBytes,bytesPerRow,NULL,NULL,(__bridge CFDictionaryRef)options,&pxbuffer);

if (block) {
    block(pxbuffer);
}
CVPixelBufferRelease(pxbuffer);
free(imageBytes);
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46513889

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档