先上代码
// 代码作用,下载图片,将图片保存到本地,再将照片设置为该页面的背景图片
// 这是uni-app的运用,为了方便在电脑做操作
downLoadImg(url) {
let that = this
return new Promise((resolve,reject)=> {
uni.downloadFile({
url: url,
success(res) {
console.log("下载成功");
resolve(res.tempFilePath)
}
})
}).then((tempFilePath)=> {
return new Promise((resolve,reject)=>{
uni.saveImageToPhotosAlbum({
filePath: tempFilePath,
success(res) {
console.log(res);
resolve(tempFilePath)
}
})
}).then(tempFilePath=>{
console.log(tempFilePath);
setTimeout(()=>{
that.src = tempFilePath
},4000)
})
}).then(()=>{
setTimeout(()=>{
console.log("over");
},2000)
})
}
上述代码的第一步,先下载图片,点进来,控制台显示如下,首先显示下载成功
紧接着,由于这是电脑,不会默认保存到电脑中,所以会打开文件夹保存
接下来,打印输出文件路径和成功的返回状态
延时2s后,输出"over"
最后改变背景照片