HarmonyOS 鸿蒙Next image加载base64图片

发布于 1周前 作者 bupafengyu 来自 鸿蒙OS

HarmonyOS 鸿蒙Next image加载base64图片

登录注册图像验证码 base64图片:/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCABQAMgDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD1CiqzXMrOUgtncg43v8iA/jyfqARUSLJciIzyzRmRN5hjUqFHHBbGQRkdxnHTrXRcCeS7jSQxIGmlHWOPBI+p6D8SM9qZ5d1OMTOsK91hbJb/AIEQMD6DPvUsJt42NtCYlaMBjEmA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46EfdJz  加载格式:  Image(data:image/[png|jpeg|bmp|webp|heif];base64,[${this.image.base64Image}]) .width(VpConst.vp40) .height(VpConst.vp20)  无法加载上面图片,上面图片是生产环境


更多关于HarmonyOS 鸿蒙Next image加载base64图片的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

加载base64图片还是用base64的格式,类似参考:

[@Entry](/user/Entry)

[@Component](/user/Component)

struct Index {

  build() {

    Column() {

      Image('data:image/png;base64,Base64编码')

        .width(100).height(100)

    }

  }

}

更多关于HarmonyOS 鸿蒙Next image加载base64图片的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中加载base64图片,可以通过以下步骤实现:

  1. 解析Base64字符串: 使用HarmonyOS提供的Base64解码功能,将Base64字符串转换为字节数组。可以通过调用common::Base64::Decode方法实现,该方法接受一个Base64字符串和一个用于存储解码后数据的输出缓冲区。

  2. 创建Bitmap对象: 使用Graphic::Bitmap类来创建一个Bitmap对象。Bitmap对象可以从字节数组初始化,通过调用Bitmap的构造函数并传入解码后的字节数组以及图片的宽度和高度(如果已知)。

  3. 将Bitmap设置为Image组件的源: 如果你正在使用UI框架中的Image组件,可以通过设置其Source属性为刚刚创建的Bitmap对象来显示图片。

示例代码(伪代码,具体实现需根据实际API调整):

// 假设base64String是包含图片数据的Base64字符串
std::string base64String = "...";
std::vector<uint8_t> decodedBytes;
common::Base64::Decode(base64String, decodedBytes);

// 假设图片宽度和高度已知,分别为width和height
int width = ...;
int height = ...;

// 创建Bitmap对象
auto bitmap = new Graphic::Bitmap(decodedBytes.data(), width, height);

// 将Bitmap设置为Image组件的源
imageComponent.SetSource(bitmap);

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部