页面加载前获取网络图片的宽高

页面加载前获取网络图片的宽高

HarmonyOS
2024-06-06 23:19:29
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
电子土豆片

示例代码

import image from '@ohos.multimedia.image'; 
 
@Entry 
@Component 
struct Index { 
  @State message: string = 'Hello World'; 
 
  @State _pixelMap: image.PixelMap | undefined = undefined; 
  @State with: number = 0 
  @State hig: number = 0 
 
  async aboutToAppear() { 
    console.error(" aboutToAppear start."); 
    let resourceManager = getContext(this).resourceManager; 
    let imageArray = await resourceManager.getMediaContent($r('app.media.startIcon')); 
    let imageResource = image.createImageSource(imageArray.buffer); 
    let opts: image.DecodingOptions = { editable: true } 
    this._pixelMap = await imageResource.createPixelMap(opts); 
    this._pixelMap.getImageInfo().then((imageInfo : image.ImageInfo) => { 
      if (imageInfo == undefined) { 
        console.error("Failed to obtain the image pixel map information."); 
      } 
      this.with = imageInfo.size.width; 
      this.hig = imageInfo.size.height; 
      console.log(" Succeeded in obtaining the image pixel map information.", this.with, this.hig); 
    }) 
    console.error(" aboutToAppear end."); 
  } 
 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
      } 
      .width(this.with) 
      .height(this.hig) 
      .backgroundColor(Color.Green) 
    } 
    .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
微信
回复
2024-06-07 21:35:07
相关问题
HarmonyOS 怎么获取网络图实际
755浏览 • 1回复 待解决
HarmonyOS 获取图片
937浏览 • 1回复 待解决
HarmonyOS 网络图片加载控件
793浏览 • 1回复 待解决
HarmonyOS Image加载网络图片
778浏览 • 1回复 待解决
HarmonyOS image加载图片自适应
1059浏览 • 1回复 待解决
HarmonyOS photoAsset获取图片失败
918浏览 • 1回复 待解决
鸿蒙如何获取Element图片
8625浏览 • 1回复 待解决
HarmonyOS 获取网络图片高度
667浏览 • 1回复 待解决
HarmonyOS ImageKnife加载网络图片失败
882浏览 • 1回复 待解决
HarmonyOS Image 加载网络图片问题
1978浏览 • 1回复 待解决
HarmonyOS ImageView不能加载网络图片
783浏览 • 1回复 待解决
Image组件如何加载网络图片
3703浏览 • 1回复 待解决
HarmonyOS 网络图片加载不显示
1459浏览 • 1回复 待解决
如何获取网络图片尺寸?
1226浏览 • 1回复 待解决
HarmonyOS 获取网络图片PixelMap
814浏览 • 1回复 待解决
关于Image组件加载网络图片问题
1756浏览 • 1回复 待解决
如何获取图片,你知道吗?
3726浏览 • 1回复 待解决
获取Column最终
1167浏览 • 1回复 待解决