使用方法
js文件
let $this = this;
request({
url:'https://www.dounine.com/hello.jpg',
method:'GET',
responseType: 'arraybuffer',
success:function(res){
let base64 = wx.arrayBufferToBase64(res);
$this.data.userImageBase64 = 'data:image/jpg;base64,' + base64;;
}
});
|
wxml文件
<image src='{{userImageBase64}}' style='width:90rpx;height:90rpx;' />
|
PS:小程序本地图片转base64最简单方法
-
wx.chooseImage:得到图片地址
-
wx.getFileSystemManager:创建文件管理类
-
readFileSync:读取本地文件,直接得到base64
wx.chooseImage({
success: function(res) {
console.log(wx.getFileSystemManager().readFileSync(res.tempFilePaths[0], "base64"))
},
})
|
附官方api:
https://developers.weixin.qq.com/miniprogram/dev/api/file/wx.getFileSystemManager.html?search-key=getFileSystemManager-[-/a>