微信小程序将图片转化为base64格式
业务需要将用户拍照的图片或者相册中选择的图片转换成base64格式,实现方法如下:
拍照/打开相册photograph的页面
// 拍照
takePhoto() {
const ctx = wx.createCameraContext()
ctx.takePhoto({
quality: 'high',
success: (res) => {
console.log(res.tempImagePath) // 为用户拍照对应的图片url
wx.redirectTo({
url: `/pages/result/result?src=${res.tempImagePath}`
})
}
})
},
// 打开相册
openFile() {
wx.chooseImage({
count: 1,
success: (res) => {
console.log(res..tempFilePaths[0]) // 为用户所选图片对应的url
wx.redirectTo({
url: `/pages/result/result?src=${res.tempFilePaths[0]}`
})
},
});
},
用户拍照结束或者选择图片后跳转到result页面,并传递图片的url,我们需要在result页面初始化的时候获取到图片的url,并将其进行base64格式化。
result页面
onLoad: function (options) {
// 将截取后的图片转换成base64格式
let ba = wx.getFileSystemManager().readFileSync(options.src, "base64")
console.log(ba)
}
总结
以上就是微信小程序中如何将一个图片转换成base64格式的实现过程,转载请填写本文详细链接,谢谢。