1 保存FTexture2DRHIRef为图片

代码如下,开箱即用:

void SaveFTexture2DRHIRefToPNG(const FTexture2DRHIRef& BackBuffer,const FString& filePath)
{
    FRHICommandListImmediate& RHICmdList = FRHICommandListExecutor::GetImmediateCommandList();
    FIntRect Rect(0, 0, BackBuffer->GetSizeX(), BackBuffer->GetSizeY());
    TArray<FColor> Data;
    RHICmdList.ReadSurfaceData(BackBuffer, Rect, Data, FReadSurfaceDataFlags(RCM_UNorm));

    // 一定要加这个,不然图片透明度一直为最大成为全透明图片
    for (FColor& color : outBMP)
    {
        color.A = 255;
    }

    TArray<uint8> compressedBitmap;
    FImageUtils::CompressImageArray(BackBuffer->GetSizeX(), BackBuffer->GetSizeY(), Data, compressedBitmap);
    FFileHelper::SaveArrayToFile(compressedBitmap, *filePath);
}