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

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

HarmonyOS
2024-05-08 23:24:14
浏览
收藏 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%’) 
} 
    } 
  } 
} 
}
分享
微博
QQ
微信
回复
2024-05-09 14:35:38
相关问题
HarmonyOS 怎么获取图片大小
12浏览 • 1回复 待解决
HarmonyOS marker无设置图片大小问题
10浏览 • 1回复 待解决
HarmonyOS Image展示本地图片失败
7浏览 • 1回复 待解决