BASE64Encoder产生的base64串带换行的,在小程序里用需要自行替换
res.data.img : 后台给到的base64格式图片
处理方法一:
let imgUrl = res.data.img;
// imgUrl = imgUrl.substr(22);
if(imgUrl.indexOf('base64,') != -1){
let imgUrlArr = imgUrl.split('base64,')//带有(data:image/png;base64,)完整路径,所以需要拆分
console.log(1);
let array_1 = wx.base64ToArrayBuffer(imgUrlArr[1])
let array_1_base64 = wx.arrayBufferToBase64(array_1)
imgUrl = imgUrlArr[0] + 'base64,' + array_1_base64;
}else{
console.log(2);
let array_1 = wx.base64ToArrayBuffer(imgUrl)
let array_1_base64 = wx.arrayBufferToBase64(array_1)
imgUrl = 'data:image/png;base64,' + array_1_base64;
}
console.log(imgUrl);
this.setData({
img : imgUrl
})
处理方法二(更简便更快的处理):
let imgUrl = res.data.img;
imgUrl = imgUrl.replace(/[\r\n]/g,"");
console.log(imgUrl);
this.setData({
img : imgUrl
})
<image src="{{img}}"></image>