我目前正在研究使用Metal的动态滤镜。在定义了我的CIImage之后,我将图像呈现为一个MTLTexture。
下面是我的渲染代码。context是一个由Metal支持的CIContext;targetTexture是附加到我的MTKView实例的currentDrawable属性的纹理的别名:
context?.render(drawImage, to: targetTexture, commandBuffer: commandBuffer, bounds: targetRect, colorSpace: colorSpace)
它渲染正确,因为我可以看到在金属视图上显示的图像。
问题是,在呈现图像(并显示它)之后,我希望提取CVPixelBuffer并使用AVAssetWriter类将其保存到磁盘。
另一种选择是有两个渲染步骤,一个渲染到纹理,另一个渲染到CVPixelBuffer。(但尚不清楚如何创建这样的缓冲区,也不清楚两个渲染步骤在帧率中会产生什么影响)
任何帮助都将不胜感激,谢谢!
发布于 2018-05-15 10:49:10
您可以尝试从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);
}发布于 2021-01-06 20:57:39
+ (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);
}https://stackoverflow.com/questions/46513889
复制相似问题