uniapp开发微信小程序时,如果有下载网络图片的需求,没有现成的接口API可用,可以分以下两步骤实现此功能。
1、使用接口uni.downloadFile(OBJECT)请求远程图片的url地址,成功的话图片会保存到本地的临时路径(返回参数里的tempFilePath)。
2、用接口uni.saveImageToPhotosAlbum(OBJECT)将刚才下载到临时目录的图片保存到相册。
这样就实现了保存网络图片的功能。
话不多说,直接上代码:
// #ifdef MP-WEIXIN
uni.downloadFile({
url: https://xxx.xxx.xxx/aaa.png, //这里是网络图片的地址
success:function(res) {
console.log(res.tempFilePath)
uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success:function(res2){
uni.showToast({
title: "图片已保存,请在相册里查看",
icon: "success"
})
}
})
}
})
// #endif