UE5 C++ 读取本地图片并赋值到UI上

目录

结果图

节点样式

主要代码

调试代码


结果图


节点样式

主要代码

(注释纯属个人理解,可能存在错误)

cpp 复制代码
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "IImageWrapper.h"
#include "IImageWrapperModule.h"
#include "GameFramework/Actor.h"
#include "SDProject01/Lib/Lib.h"
#include "LoadLocalPic.generated.h"

UCLASS()
class SDPROJECT01_API ALoadLocalPic : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ALoadLocalPic();
	UFUNCTION(BlueprintCallable,Category="MyFunction2Load",meta=(Keywords="Load Image"))
	int testFunc(int a, int b);

	UFUNCTION(BlueprintCallable, Category = "MyFunction2Load", meta = (Keywords = "Load Image"))
	UTexture2D* LoadImageFromFile(const FString& ImagePath);
};
cpp 复制代码
// Fill out your copyright notice in the Description page of Project Settings.


#include "LoadLocalPic.h"

// Sets default values
ALoadLocalPic::ALoadLocalPic()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

}
int ALoadLocalPic::testFunc(int a, int b)
{
	return a + b;
}

UTexture2D* ALoadLocalPic::LoadImageFromFile(const FString& ImagePath)
{
	//判断地址为空
	if (ImagePath.IsEmpty()) {

		Lib::echo("Path is empty", 3);
		return nullptr;
	}

	//判断是否存在文件,文件是否能转为数组
	TArray<uint8> CompressedData;
	if (!FFileHelper::LoadFileToArray(CompressedData, *ImagePath)) {
		Lib::echo("file not find",3);
		return nullptr;
	}

	//判断文件格式
	EImageFormat imageformat = EImageFormat::Invalid;
	if (ImagePath.EndsWith(".png"))
		imageformat = EImageFormat::PNG;
	else if (ImagePath.EndsWith(".jpg") || ImagePath.EndsWith(".jpeg"))
		imageformat = EImageFormat::JPEG;
	else
	{
		Lib::echo("fileformat false", 3);
		return nullptr;
	}

	//创建图片封装器
	IImageWrapperModule& imageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("IMageWrapper"));
	TSharedPtr<IImageWrapper> imageWrapper = imageWrapperModule.CreateImageWrapper(imageformat);

	//解码图片
	//获取图片信息
	if (!imageWrapper->SetCompressed(CompressedData.GetData(), CompressedData.Num())) {

		Lib::echo("Compressed file failed", 3);
		return nullptr;
	}

	//创建纹理
	TArray<uint8> UncompressedRGBA;
	if (!imageWrapper->GetRaw(ERGBFormat::RGBA, 8, UncompressedRGBA))
		return nullptr;

	UTexture2D* texture2d= UTexture2D::CreateTransient(imageWrapper->GetWidth(), imageWrapper->GetHeight(), PF_R8G8B8A8);
	if (!texture2d)
		return nullptr;

	//赋值纹理
	void* texturedata = texture2d->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
	FMemory::Memcpy(texturedata, UncompressedRGBA.GetData(), UncompressedRGBA.Num());
	texture2d->PlatformData->Mips[0].BulkData.Unlock();

	texture2d->UpdateResource();
	return texture2d;
}

调试代码

cpp 复制代码
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"

/**
 * 
 */
class SDPROJECT01_API Lib
{
public:
	Lib();
	~Lib();
	static void echo(FString value, float duration);
	static void echo(float value, float duration);
};
cpp 复制代码
// Fill out your copyright notice in the Description page of Project Settings.


#include "SDProject01/Lib/Lib.h"

Lib::Lib()
{
}

Lib::~Lib()
{
}

void Lib::echo(FString value, float duration)
{

	GEngine->AddOnScreenDebugMessage(-1, duration, FColor::Blue, value);
}

void Lib::echo(float value, float duration)
{
	const FString temp = FString::Printf(TEXT("%f"), value);
	GEngine->AddOnScreenDebugMessage(-1,duration,FColor::Blue,temp);
}
相关推荐
AA陈超2 天前
虚幻引擎5 GAS开发俯视角RPG游戏 P06-29 属性信息委托
c++·游戏·ue5·游戏引擎·虚幻
AA陈超2 天前
虚幻引擎5 GAS开发俯视角RPG游戏 P06-31 映射标签到属性
c++·游戏·ue5·游戏引擎·虚幻
gshh__2 天前
SuperMap Hi-Fi 3D SDK for Unreal 使用蓝图接口加载多源数据
ue5·游戏引擎·supermap
zhangzhangkeji3 天前
cesium126,230331,Visualize Per-Feature Metadata - 1:官方教程
ue5
zhangzhangkeji3 天前
cesium126,230316,根据经纬度动态生成物体:主要使用了角色的 tag 属性,地球锚点也是有 tag 属性的
ue5
AA陈超3 天前
虚幻引擎5 GAS开发俯视角RPG游戏 P06-28 构建属性菜单小部件控制器
c++·游戏·ue5·游戏引擎·虚幻
zhangzhangkeji3 天前
UE5 材质-17:水材质系列一 ,panner 平移节点,
ue5·材质
zhangzhangkeji7 天前
UE5 蓝图-24:主 mainUI界面蓝图,主菜单按钮事件定义,拆分按钮,color按钮,退出按钮
ue5
zhangzhangkeji7 天前
UE5 蓝图-11:本汽车蓝图的事件图表,汽车拆分事件,染色事件(绿蓝黄青)。
ue5·1024程序员节
zhangzhangkeji8 天前
UE5 材质-18:水材质系列二 ,水岸接缝的美化节点 DepthFade,水面法线混合节点 BlendAngleCorrectedNormals
ue5