
第一种,普通图片路径
uni.downloadFile({
url: this.yaoqinphoto,
success: (res) => {
console.log(res)
uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: function() {
uni.showToast({
title: '保存成功',
icon: 'none'
})
},
fail(err) {
uni.showToast({
title: '下载失败',
icon: 'none'
})
}
})
},
fail(err) {
console.log(err)
}
});
第二种,图片是 base64
// 图片是base64
let base64=this.yaoqinphoto.replace(/^data:image\/\w+;base64,/, "");//去掉data:image/png;base64,
let filePath=wx.env.USER_DATA_PATH + '/hym_pay_qrcode.png';
uni.getFileSystemManager().writeFile({
filePath:filePath , //创建一个临时文件名
data: base64,
encoding: 'base64', //写入当前文件的字符编码
success: res => {
uni.saveImageToPhotosAlbum({
filePath: filePath,
success: function(res2) {
uni.showToast({
title: '保存成功,请从相册选择再分享',
icon:"none",
duration:5000
})
},
fail: function(err) {
uni.showToast({
title: '下载失败',
icon: 'none'
});
}
})
},
fail: err => {
//console.log(err)
uni.showToast({
title: '下载失败',
icon: 'none'
});
}
})
文章介绍了在uni-app中两种下载并保存图片的方法,一种是处理普通图片路径,使用uni.downloadFile结合uni.saveImageToPhotosAlbum;另一种是处理base64格式的图片,先转换再利用getFileSystemManager.writeFile保存,然后同样保存到相册。
1万+

被折叠的 条评论
为什么被折叠?



