参考:
谢谢这位大哥,按照你的讲解,做了一个出来。
https://www.bilibili.com/video/BV1UE411Z7uY?t=1496
步骤一:
添加模块引用
PrivateDependencyModuleNames.AddRange(
new string[]
{
"CoreUObject",
"Engine",
"Slate",
"SlateCore",
"ImageWrapper"
// ... add private dependencies that you statically link with here ...
}
);
截图如下:
第二步:
加载图片到纹理
static TSharedPtr<IImageWrapper> GetImageWrapperByExtention(const FString path)
{
IImageWrapperModule& module = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
if (path.EndsWith(".png"))
{
return module.CreateImageWrapper(EImageFormat::PNG);
}
if (path.EndsWith(".jpg"))
{
return module.CreateImageWrapper(EImageFormat::JPEG);
}
return nullptr;
}
UTexture2D* UReadimageBpBPLibrary::LoadTextrue2D(const FString path, bool& IsValid, int32& OutWidth, int32& OutHeight)
{
UTexture2D* Texture = nullptr;
IsValid = false;
if (!FPlatformFileManager::Get().GetPlatformFile().FileExists(*path))
{
return nullptr;
}
TArray<uint8> CompressedData;
if (!FFileHelper::LoadFileToArray(CompressedData, *path))
{
return nullptr;
}
TSharedPtr<IImageWrapper> ImageWrapper = GetImageWrapperByExtention(path);
if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(CompressedData.GetData(), CompressedData.Num()))
{
TArray<uint8> UncompressedRGBA;
if (ImageWrapper->GetRaw(ERGBFormat::RGBA, 8, UncompressedRGBA))
{
Texture = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_R8G8B8A8);
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;
}
第三步:创建一个蓝图User Interface节点
第四步:在蓝图节点里面添加一个图片控件
第五步:编写蓝图逻辑
第六步:我就截个图
蓝图的逻辑就截个图吧,比较多
第七部:运行效果,成功通过C++代码读取图片,并显示