如何先获取图片图片大小,然后缩放处理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
相关问题
服务卡片image怎么获取网络图片
6709浏览 • 2回复 待解决
如何用PixelMap处理图片
1387浏览 • 1回复 待解决
Image组件如何加载网络图片
2301浏览 • 1回复 待解决
HarmonyOS图片压缩不到指定大小
351浏览 • 1回复 待解决
Image怎么替换svg图片
8105浏览 • 1回复 待解决