如何先获取图片图片大小,然后缩放处理pixleMap,最后通过image展示

如何先获取图片图片大小,然后缩放处理pixleMap,最后通过image展示

HarmonyOS
2024-05-08 23:24:14
2125浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
depengli

使用一下demo:

import image from ‘@ohos.multimedia.image’; 
import { BusinessError } from ‘@kit.BasicServicesKit’; 
 
@Entry 
@Component 
struct Index { 
  @State pixelMap: image.PixelMap | undefined = undefined 
  @State isChange: Boolean = false 
  @State xScale: number = 0.5 
  @State yScale: number = 0.5 
 
  async aboutToAppear() { 
let resourceMgr = getContext(this).resourceManager; 
let imageArray = await resourceMgr.getMediaContent($r(‘app.media.picture’)); 
let imageResource = image.createImageSource(new Uint8Array(imageArray).buffer as Object as ArrayBuffer); 
let opts: image.DecodingOptions = { editable: true }; 
  this.pixelMap = await imageResource.createPixelMap(opts); 
  this.pixelMap.scale(this.xScale, this.yScale).then(() => { 
  this.isChange = !this.isChange 
}).catch((error: BusinessError) => { 
  console.error("Error occurred when scaling, error message: " + error.message); 
}) 
 
} 
 
build() { 
  Navigation(){ 
    Stack(){ 
      Column(){ 
Column(){ 
Image(this.pixelMap? this.pixelMap : ‘’) 
.objectFit(ImageFit.None) 
} 
.width(‘100%’) 
.height(‘70%’) 
} 
    } 
  } 
} 
}
  • 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.
分享
微博
QQ
微信
回复
2024-05-09 14:35:38
相关问题
HarmonyOS 怎么获取图片大小
630浏览 • 1回复 待解决
HarmonyOS 相册选择后压缩图片大小
810浏览 • 1回复 待解决
HarmonyOS marker无设置图片大小问题
624浏览 • 1回复 待解决
HarmonyOS Image展示本地图片失败
1120浏览 • 1回复 待解决