4

我在将 MTLTexture 转换为 CVMetalTextureRef 或 CVPixelBufferRef 时遇到了问题,我的代码是这样的:

CVPixelBufferLockBaseAddress(_screenPixelBuffer, 0);//必须锁定内存

    id <MTLTexture> outputTexture = drawable.texture;

    MTLRegion region = MTLRegionMake2D(0, 0, drawable.layer.drawableSize.width, drawable.layer.drawableSize.height);

    int bytesPerPixel = 4;
    int bytesPerRow = bytesPerPixel * drawable.layer.drawableSize.width;

    NSLog(@"drawable.layer.drawableSize.width = %f, drawable.layer.drawableSize.height = %f", drawable.layer.drawableSize.width, drawable.layer.drawableSize.height);
    NSLog(@"textureType = %lu, width = %lu, height = %lu, depth = %lu, arrayLength = %lu, mipmapLevelCount = %lu, sampleCount = %lu", outputTexture.textureType, outputTexture.width, outputTexture.height, outputTexture.depth, outputTexture.arrayLength, outputTexture.mipmapLevelCount, outputTexture.sampleCount);

    void *tmpBuffer = CVPixelBufferGetBaseAddress(_screenPixelBuffer);

    [outputTexture getBytes:tmpBuffer bytesPerRow:bytesPerRow fromRegion:region mipmapLevel:0];


    [_iv setImage:[self imageFromPixelBuffer:_screenPixelBuffer]];

    CVPixelBufferUnlockBaseAddress(_screenPixelBuffer, 0);//解锁内存

但是 UIImageview 显示的图像不正确。完整的代码在这里

4

0 回答 0