多云转晴
背景:订单需要店主拍照并上传在用户端展示,获取网络图片原有的高进行展示
直接上代码
setImgUrlsAndHeight(imgUrls) {
if (!imgUrls || imgUrls.length === 0) {return;}
const promises = imgUrls.map((img, index) => {
return new Promise((resolve, reject) => {
Image.getSize(img, (width, height) => {
let imgScale = width / height;
let imgW = globalConstants.screen_width - (24/ratio_width);
let imgH = imgW/imgScale;
resolve(imgH);
}, (error) => {
reject(error);
});
});
});
Promise.all(promises).then((imgHeights) => {
this.setState({imgHeights: imgHeights});
}, (errors) => {
if (errors && errors.length > 0) {
Toast.showToast('图片加载失败');
this.setState({imgHeights: [0,0,0]});
}
})
}