HarmonyOS 用Image显示了网络图片,可以拿到Image组件用的图片的PixelMap吗

需要拿到Image()用的网络图片,使用effectKit获取所用图片的主色调。如果不能直接用Image()组件已经下载好的图,是需要自己单独下载么,或有其他的办法获取Image()图片的主色调的方法?

HarmonyOS
2025-01-09 17:31:07
615浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

需要先下载网络图片再获取PixelMap,参考示例如下:

@State image: PixelMap | undefined = undefined;
httpRequest() {
  http.createHttp().request(
    "https://xxx",
    (error: BusinessError, data: http.HttpResponse) => {
      if (error) {
        console.error(`http reqeust failed with. Code: ${error.code}, message: ${error.message}`);
      } else {
        console.error('http reqeust success.');
        let imageData: ArrayBuffer = data.result as ArrayBuffer;
        let imageSource: image.ImageSource = image.createImageSource(imageData);
        console.error(`http reqeust size = ${imageData.byteLength}`);

        class tmp {
          height: number = 100
          width: number = 100
        }

        let options: Record<string, number | boolean | tmp> = {
          'alphaType': 0, // 透明度
          'editable': false, // 是否可编辑
          'pixelFormat': 3, // 像素格式
          'scaleMode': 1, // 缩略值
          'size': { height: 100, width: 100 }
        } // 创建图片大小
        imageSource.createPixelMap(options).then((pixelMap: PixelMap) => {
          this.image = pixelMap
        })
      }
    }
  )
}

build() {
  Column() {
    Button("获取网络图片")
      .onClick(() => {
        this.httpRequest()
      })
    Image(this.image)
      .height(200).width(200)
  }.width('100%').height('100%')
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
分享
微博
QQ
微信
回复
2025-01-09 19:02:12


相关问题
HarmonyOS Image组件网络图片显示
1220浏览 • 1回复 待解决
HarmonyOS Image组件无法显示网络图片
2145浏览 • 1回复 待解决
HarmonyOS Image组件网络图片缓存
927浏览 • 1回复 待解决
HarmonyOS Image加载网络图片
596浏览 • 1回复 待解决
Image组件如何加载网络图片
3403浏览 • 1回复 待解决
关于Image组件加载网络图片问题
1535浏览 • 1回复 待解决
HarmonyOS Image 加载网络图片问题
1751浏览 • 1回复 待解决
服务卡片image怎么获取网络图片
7910浏览 • 2回复 待解决
请问Image控件是怎样设置网络图片
11077浏览 • 3回复 已解决
恭喜您,今日已阅读两篇内容,特奖励+2声望, 快来领取吧。