UE4将图片转换为Texture2D,并通过Slate显示

测试环境:Win10+虚幻版本v4.19.2

#include "SMyTestWidget.h"

#include "SlateOptMacros.h"
#include "SBoxPanel.h"
#include "SImage.h"
#include "STextBlock.h"

#include "Runtime/Engine/Classes/Engine/Texture2D.h"
#include "Runtime/ImageWrapper/Public/IImageWrapperModule.h"
#include "Runtime/SlateCore/Public/Brushes/SlateDynamicImageBrush.h"
#include "Runtime/Core/Public/Misc/FileHelper.h"
#include "Runtime/ImageWrapper/Public/IImageWrapper.h"
#include "Runtime/ImageWrapper/Public/IImageWrapperModule.h"
#include "Runtime/Core/Public/Modules/ModuleManager.h"
#include "Runtime/Engine/Classes/Engine/Texture2D.h"
#include "Runtime/Core/Public/HAL/PlatformFilemanager.h"

BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION
void SMyTestWidget::Construct(const FArguments& InArgs)
{
    //图片路径
    FString ImagePath = TEXT("D:/EpicGames/projects/testUI/Plugins/testDocktab/Resources/qq2.png");

    bool isSuccess;
    int32 Width;
    int32 Height;
    UTexture2D* NewTexture = LoadTexture2D(ImagePath, isSuccess, Width, Height);
    if (isSuccess)
    {
        ChildSlot
            [
                SNew(SVerticalBox)
                + SVerticalBox::Slot().HAlign(HAlign_Center).VAlign(VAlign_Center)
            [
                SNew(SImage)
                .Image(new FSlateDynamicImageBrush(NewTexture, FVector2D(128, 128), FName("qq2")))
            ]
        + SVerticalBox::Slot().HAlign(HAlign_Center).VAlign(VAlign_Top)
            [
                SNew(STextBlock).Text(NSLOCTEXT("test", "test", "test"))
            ]
            ];
    }

}

UTexture2D* SMyTestWidget::LoadTexture2D(const FString& ImagePath, bool& IsValid, int32& OutWidth, int32& OutHeight)
{
    UTexture2D* Texture = nullptr;
    IsValid = false;
    if (!FPlatformFileManager::Get().GetPlatformFile().FileExists(*ImagePath))
    {
        return nullptr;
    }
    TArray<uint8> CompressedData;
    if (!FFileHelper::LoadFileToArray(CompressedData, *ImagePath))
    {
        return nullptr;
    }
    TSharedPtr ImageWrapper = GetImageWrapperByExtention(ImagePath);
    if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(CompressedData.GetData(), CompressedData.Num()))
    {
        const TArray<uint8>* UncompressedRGBA = nullptr;
        //必须是BGRA通道格式,否则无法正确加载jpg格式文件
        if (ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, UncompressedRGBA))
        {
            Texture = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_B8G8R8A8);
            if (Texture != nullptr)
            {
                IsValid = true;
                OutWidth = ImageWrapper->GetWidth();
                OutHeight = ImageWrapper->GetHeight();
                void* TextureData = Texture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
                FMemory::Memcpy(TextureData, UncompressedRGBA->GetData(), UncompressedRGBA->Num());
                Texture->PlatformData->Mips[0].BulkData.Unlock();
                Texture->UpdateResource();
            }
        }
    }
    return Texture;
}
//根据文件后缀返回对应IImageWrapper
TSharedPtr SMyTestWidget::GetImageWrapperByExtention(const FString InImagePath)
{
    IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked(FName("ImageWrapper"));
    if (InImagePath.EndsWith(".png"))
    {
        return ImageWrapperModule.CreateImageWrapper(EImageFormat::PNG);
    }
    else if (InImagePath.EndsWith(".jpg") || InImagePath.EndsWith(".jpeg"))
    {
        return ImageWrapperModule.CreateImageWrapper(EImageFormat::JPEG);
    }
    else if (InImagePath.EndsWith(".bmp"))
    {
        return ImageWrapperModule.CreateImageWrapper(EImageFormat::BMP);
    }
    else if (InImagePath.EndsWith(".ico"))
    {
        return ImageWrapperModule.CreateImageWrapper(EImageFormat::ICO);
    }
    else if (InImagePath.EndsWith(".exr"))
    {
        return ImageWrapperModule.CreateImageWrapper(EImageFormat::EXR);
    }
    else if (InImagePath.EndsWith(".icns"))
    {
        return ImageWrapperModule.CreateImageWrapper(EImageFormat::ICNS);
    }
    return nullptr;
}

END_SLATE_FUNCTION_BUILD_OPTIMIZATION

你可能感兴趣的:(UE4开发,积累)