HarmonyOS 获取网络图片高度

根据网络图片的高度来给 image 组件设置高度

HarmonyOS
2025-01-09 15:02:51
854浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa

可以参考以下demo:

import http from '@ohos.net.http';
import image from '@ohos.multimedia.image';
import { BusinessError } from '@ohos.base';

@Entry
@Component
struct ImageToPixelMap {
  @State image: PixelMap | undefined = undefined;
  private imageWith: number = 0;
  private imageHeight: Length = 100;

  aboutToAppear(): void {
    this.httpRequest()
  }

  build() {
    Column() {
      Image(this.image)
        .height(this.imageHeight).width(this.imageWith)

    }.width('100%').height('100%')
  }

  httpRequest() {
    http.createHttp().request(
      "https://xxx.jpg",
      (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}`);

          let opts: image.DecodingOptions = { editable: true }
          let pixelMap: image.PixelMap = imageSource.createPixelMapSync(opts);
          pixelMap.getImageInfo().then((imageInfo: image.ImageInfo) => {
            if (imageInfo == undefined) {
              console.error("Failed to obtain the image pixel map information.");
            }
            this.imageWith = imageInfo.size.width;
            this.imageHeight = imageInfo.size.height;
            console.log("Succeeded in obtaining the image pixel map information.", this.imageWith,
              this.imageHeight);
          })

          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
          })
        }
      }
    )
  }
}
  • 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.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
分享
微博
QQ
微信
回复
2025-01-09 16:54:23


相关问题
HarmonyOS 获取网络图片PixelMap
1018浏览 • 1回复 待解决
如何获取网络图片的尺寸?
1354浏览 • 1回复 待解决
HarmonyOS 网络图片加载控件
914浏览 • 1回复 待解决
HarmonyOS Image加载网络图片
928浏览 • 1回复 待解决
服务卡片image怎么获取网络图片
8409浏览 • 2回复 待解决
获取网络图片并保存到相册
3209浏览 • 1回复 待解决
页面加载前获取网络图片的宽高
1650浏览 • 1回复 待解决
HarmonyOS 请求网络图片
1313浏览 • 1回复 待解决
HarmonyOS Image组件网络图片不显示
1692浏览 • 1回复 待解决
如何保存网络图片到相册
1986浏览 • 1回复 待解决
服务卡片怎么显示网络图片
584浏览 • 0回复 待解决
HarmonyOS ImageView不能加载网络图片
930浏览 • 1回复 待解决
HarmonyOS 保存网络图片,图库更新
1224浏览 • 1回复 待解决
HarmonyOS Image 加载网络图片问题
2214浏览 • 1回复 待解决
HarmonyOS ImageKnife加载网络图片失败
1093浏览 • 1回复 待解决
HarmonyOS 使用picker保存网络图片
778浏览 • 1回复 待解决
HarmonyOS 网络图片加载不显示
1921浏览 • 1回复 待解决
HarmonyOS Image组件的网络图片缓存
1224浏览 • 1回复 待解决