#鸿蒙通关秘籍#如何在HarmonyOS中进行多媒体像素图显示?

HarmonyOS
2024-12-04 15:05:36
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
代码小天才

可以先通过网络获取图片数据,再将其解码为PixelMap格式,并赋值给Image组件。实现代码如下:

@State image: PixelMap | undefined = undefined;

import { http } from '@kit.NetworkKit';
import { image } from '@kit.ImageKit';
import { BusinessError } from '@kit.BasicServicesKit';

let OutData: http.HttpResponse;
http.createHttp().request("https://www.example.com/xxx.png",
  (error: BusinessError, data: http.HttpResponse) => {
    if (error) {
      console.error(`http request failed with. Code: ${error.code}, message: ${error.message}`);
    } else {
      OutData = data;
      let imageData: ArrayBuffer = OutData.result as ArrayBuffer;
      let imageSource: image.ImageSource = image.createImageSource(imageData);
      let options = { 'alphaType': 0, 'editable': false, 'pixelFormat': 3, 'scaleMode': 1, 'size': { height: 100, width: 100 } };

      imageSource.createPixelMap(options).then((pixelMap: PixelMap) => {
        this.image = pixelMap;
      });
    }
  }
);

Image(this.image).height(100).width(100);
分享
微博
QQ
微信
回复
2024-12-04 17:04:24
相关问题
HarmonyOS 多媒体相关demo
176浏览 • 1回复 待解决
如何在ArkTS中进行模块化开发?
561浏览 • 1回复 待解决