#鸿蒙学习大百科#如何将网络图片转换为pixelMap?

如何将网络图片转换为pixelMap?

HarmonyOS
2024-10-25 09:54:59
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
重庆大镖客
import { http } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { image } from '@kit.ImageKit';

let OutData: http.HttpResponse
@Entry
@Component
struct Index {

  aboutToAppear(): void {
    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 code: http.ResponseCode | number = OutData.responseCode
          if (http.ResponseCode.OK === code) {
            let imageData: ArrayBuffer = OutData.result as ArrayBuffer;
            let imageSource: image.ImageSource = image.createImageSource(imageData);
            class tmp {
              height: number = 100
              width: number = 100
            }
            let si: tmp = new tmp()
            let options: Record<string, number | boolean | tmp> = {
              'alphaType': 0, // 透明度
              'editable': false, // 是否可编辑
              'pixelFormat': 3, // 像素格式
              'scaleMode': 1, // 缩略值
              'size': { height: 100, width: 100 }
            } // 创建图片大小
            class imagetmp {
              image: PixelMap | undefined = undefined
              set(val: PixelMap) {
                this.image = val
              }
            }
            imageSource.createPixelMap(options).then((pixelMap: PixelMap) => {
              let im = new imagetmp()
              im.set(pixelMap)
            })
          }
        }
      }
    )
  }

  build() {
    Column() {

    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}
分享
微博
QQ
微信
回复
2024-10-25 16:38:29
相关问题
如何将app.media.app_icon,转换为PixelMap
1903浏览 • 1回复 待解决
HarmonyOS 如何将base64数据转换为图片
518浏览 • 1回复 待解决
HarmonyOS如何将Component转换PixelMap
398浏览 • 1回复 待解决