微信小程序如何獲取base64圖片的寬高?
相信大家很多時候都遇到過需要獲取base64圖片寬高的時候,在js中獲取。
let base64 = 'data:image/png;base64,........';
let img = new Image();
img.onload = function(){
console.log('height',img.height);
console.log('width',img.width);
}
img.src = base64;
通過這樣就可以獲取到base64的寬高
當在小程序中需要獲取base64圖片寬高的時候,查了查微信小程序的api文檔也沒有滿足這個情況的方法。
最后找到一個小程序image組件的一個bindload方法。
最后解決方法如下:
<image bindload="imgBindload" class="sign_page_img" src="{{pageItem.base64}}" mode="widthFix"></image>
// 獲取原始圖片的寬高--pdf寬高
imgBindload(e) {
// 阻止圖片加載每次都執行。如果是循環出來的圖片的話,只執行第一次。
if (this.data.pdfWidth) {
return false
}
this.data.pdfWidth = e.detail.width
this.data.pdfHeight = e.detail.height
//干點其他的
},