从手机上选择图片转base64
getToBase64(){
wx.chooseImage({
count:'1', // 最多可以选择的图片张数
sizeType: ['original', 'compressed'], // ['原图','压缩图']
sourceType: ['album', 'camera'], // ['从相册选图','使用相机']
success: res => {
wx.getFileSystemManager().readFile({
filePath: res.tempFilePaths[0], //选择图片返回的相对路径
encoding: 'base64', //编码格式
success: res => { //成功的回调
console.log('data:image/png;base64,' + res.data)
}
})
}
})
},
网络图片转base64
getToBase64Two(){
var httpsimg = 'https://baidu.com/a.jpg' // 网络图片地址
wx.downloadFile({ // 需要先下载
url: httpsimg,
success(res) {
console.log(res,'res')
wx.getFileSystemManager().readFile({
filePath: res.tempFilePath, //选择图片返回的相对路径
encoding: 'base64', //编码格式:base64 | hex(16进制)
success: res => { //成功的回调
let userImageBase64 = 'data:image/jpg;base64,' + res.data;
console.log(userImageBase64); // 打印base64格式图片
}
})
}
})
},