0


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

结果图


节点样式

主要代码

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

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. #include "IImageWrapper.h"
  5. #include "IImageWrapperModule.h"
  6. #include "GameFramework/Actor.h"
  7. #include "SDProject01/Lib/Lib.h"
  8. #include "LoadLocalPic.generated.h"
  9. UCLASS()
  10. class SDPROJECT01_API ALoadLocalPic : public AActor
  11. {
  12. GENERATED_BODY()
  13. public:
  14. // Sets default values for this actor's properties
  15. ALoadLocalPic();
  16. UFUNCTION(BlueprintCallable,Category="MyFunction2Load",meta=(Keywords="Load Image"))
  17. int testFunc(int a, int b);
  18. UFUNCTION(BlueprintCallable, Category = "MyFunction2Load", meta = (Keywords = "Load Image"))
  19. UTexture2D* LoadImageFromFile(const FString& ImagePath);
  20. };
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #include "LoadLocalPic.h"
  3. // Sets default values
  4. ALoadLocalPic::ALoadLocalPic()
  5. {
  6. // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
  7. PrimaryActorTick.bCanEverTick = true;
  8. }
  9. int ALoadLocalPic::testFunc(int a, int b)
  10. {
  11. return a + b;
  12. }
  13. UTexture2D* ALoadLocalPic::LoadImageFromFile(const FString& ImagePath)
  14. {
  15. //判断地址为空
  16. if (ImagePath.IsEmpty()) {
  17. Lib::echo("Path is empty", 3);
  18. return nullptr;
  19. }
  20. //判断是否存在文件,文件是否能转为数组
  21. TArray<uint8> CompressedData;
  22. if (!FFileHelper::LoadFileToArray(CompressedData, *ImagePath)) {
  23. Lib::echo("file not find",3);
  24. return nullptr;
  25. }
  26. //判断文件格式
  27. EImageFormat imageformat = EImageFormat::Invalid;
  28. if (ImagePath.EndsWith(".png"))
  29. imageformat = EImageFormat::PNG;
  30. else if (ImagePath.EndsWith(".jpg") || ImagePath.EndsWith(".jpeg"))
  31. imageformat = EImageFormat::JPEG;
  32. else
  33. {
  34. Lib::echo("fileformat false", 3);
  35. return nullptr;
  36. }
  37. //创建图片封装器
  38. IImageWrapperModule& imageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("IMageWrapper"));
  39. TSharedPtr<IImageWrapper> imageWrapper = imageWrapperModule.CreateImageWrapper(imageformat);
  40. //解码图片
  41. //获取图片信息
  42. if (!imageWrapper->SetCompressed(CompressedData.GetData(), CompressedData.Num())) {
  43. Lib::echo("Compressed file failed", 3);
  44. return nullptr;
  45. }
  46. //创建纹理
  47. TArray<uint8> UncompressedRGBA;
  48. if (!imageWrapper->GetRaw(ERGBFormat::RGBA, 8, UncompressedRGBA))
  49. return nullptr;
  50. UTexture2D* texture2d= UTexture2D::CreateTransient(imageWrapper->GetWidth(), imageWrapper->GetHeight(), PF_R8G8B8A8);
  51. if (!texture2d)
  52. return nullptr;
  53. //赋值纹理
  54. void* texturedata = texture2d->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
  55. FMemory::Memcpy(texturedata, UncompressedRGBA.GetData(), UncompressedRGBA.Num());
  56. texture2d->PlatformData->Mips[0].BulkData.Unlock();
  57. texture2d->UpdateResource();
  58. return texture2d;
  59. }

调试代码

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. /**
  5. *
  6. */
  7. class SDPROJECT01_API Lib
  8. {
  9. public:
  10. Lib();
  11. ~Lib();
  12. static void echo(FString value, float duration);
  13. static void echo(float value, float duration);
  14. };
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #include "SDProject01/Lib/Lib.h"
  3. Lib::Lib()
  4. {
  5. }
  6. Lib::~Lib()
  7. {
  8. }
  9. void Lib::echo(FString value, float duration)
  10. {
  11. GEngine->AddOnScreenDebugMessage(-1, duration, FColor::Blue, value);
  12. }
  13. void Lib::echo(float value, float duration)
  14. {
  15. const FString temp = FString::Printf(TEXT("%f"), value);
  16. GEngine->AddOnScreenDebugMessage(-1,duration,FColor::Blue,temp);
  17. }
标签: ue5

本文转载自: https://blog.csdn.net/weixin_56537692/article/details/135943173
版权归原作者 我的巨剑能轻松搅动潮汐 所有, 如有侵权,请联系我们删除。

“UE5 C++ 读取本地图片并赋值到UI上”的评论:

还没有评论