最近在开发一个功能。如果接口返回的图片高度大于当前手机屏幕的高度,则图片可以滚动。否则,让图片充满整个屏幕并禁止滚动。方法是wx.getImageInfo,微信官方文档获取图片宽高的代码:wx.getImageInfo({src:this.data.info.imgUrl,complete:(res)=>{//The原图宽度letimgWidth=res.width;//原图高度letimgHeight=res.height;//设备宽度letscreenWidth=wx.getSystemInfoSync().windowWidth;//高度thedeviceletscreenHeight=wx.getSystemInfoSync().windowHeight;//7502000//图片的宽度除以屏幕的宽度letratio2=imgWidth/screenWidth;//得到渲染图片的高度letsaleHight=(imgHeight/ratio2).toFixed(2);if(saleHight>=screenHeight){this.setData({['info.width']:screenWidth+'px',['info.height']:saleHight+'px',['info.mode']:'aspectFit',});}else{this.setData({['info.width']:'100%',['info.height']:'100%',['info.mode']:'aspectFill',});}}})循环获取图片宽高的代码:this.data.list.forEach((item,index)=>{wx.getImageInfo({src:item.info.imgUrl,complete:(res)=>{//原图的宽度letimgWidth=res.width;//原图的高度letimgHeight=res.height;letscreenWidth=wx.getSystemInfoSync().windowWidth;letscreenHeight=wx.getSystemInfoSync().windowHeight;//7502000//图片的宽度除以屏幕的宽度letratio2=imgWidth/screenWidth;//得到渲染图片的高度letsaleHight=(imgHeight/ratio2).toFixed(2);if(saleHeight>=screenHeight){this.setData({[`list[${index}]info.width`]:screenWidth+'px',[`list[${index}]info.height`]:saleHight+'px',[`list[${index}]info.mode`]:'aspectFit',});}else{this.setData({[`list[${index}]info.width`]:'100%',[`list[${index}]info.height`]:'100%',[`list[${index}]info.mode`]:'aspectFill',});}}})})实现我想要的功能,需要注意wx.getImageInfo是异步的
