describe
When working on a project, a QR code is generated by filling in personal information. Now it is required to save the generated QR code locally. When clicking "Save to Local" on the picture, the picture will be saved to the local album
Implementation process
The following code picUrl
is in base64 format, you need to remove the previous "data:image/png;base64" section before you can use the next called api
saveBase64Img(){
console.log("picUrl")
console.log(this.picUrl)//
let base64=this.picUrl.replace(/^data:image\/\w+;base64,/, "");//去掉data:image/png;base64,
let filePath=wx.env.USER_DATA_PATH + '/hym_pay_qrcode.png';
var save = uni.getFileSystemManager();
var number = Math.random();
save.writeFile({
filePath: filePath,
data:base64,
encoding: 'base64',
success: res => {
console.log(669699, res)
uni.saveImageToPhotosAlbum({
filePath: filePath,
success: function (res) {
uni.showToast({
title: '保存成功',
icon: "success",
duration: 1000
})
},
fail: function (err) {
uni.showToast({
title: '保存失败',
icon: "success",
duration: 1000
})
console.log(err)
}
})
console.log(res)
},
fail: err => {
console.log(err)
}
})
}
Reference article:
http://www.zhangkeda.com/archives/418.html