开发过程中需要保存到相册,可以使用这个方法,要注意的是需要配置downloadFile合法域名
//保存到相册
savePoster(){
letthat=this
wx.getSetting({
success(res){
if(res.authSetting['scope.writePhotosAlbum']){
that.saveImg();
}elseif(res.authSetting['scope.writePhotosAlbum']===undefined){
wx.authorize({
scope:'scope.writePhotosAlbum',
success(){
that.saveImg();
},
fail(){
that.authConfirm()
}
})
}else{
that.authConfirm()
}
}
})
},
//授权拒绝后,再次授权提示弹窗
authConfirm(){
letthat=this
wx.showModal({
content:'检测到您没打开保存图片权限,是否去设置打开?',
confirmText:"确认",
cancelText:"取消",
success:function(res){
if(res.confirm){
wx.openSetting({
success(res){
if(res.authSetting['scope.writePhotosAlbum']){
that.saveImg();
}else{
wx.showToast({
title:'您没有授权,无法保存到相册',
icon:'none'
})
}
}
})
}else{
wx.showToast({
title:'您没有授权,无法保存到相册',
icon:'none'
})
}
}
});
},
//保存图片
saveImg:function(){
varthat=this;
varimgUrl=that.data.imgurl;//需要保存的图片地址
console.log(imgUrl)
wx.downloadFile({
url:imgUrl,
success(res){
//只要服务器有响应数据,就会把响应内容写入文件并进入success回调,业务需要自行判断是否下载到了想要的内容
console.log('downloadFileres',res);
wx.saveImageToPhotosAlbum({
filePath:res.tempFilePath,
success(res){
console.log("保存图片:success");
wx.showToast({
title:'保存成功',
});
},
fail:res=>{
console.log(res);
wx.showToast({
title:'保存失败',
});
}
})
}
})
},点击 "运行实例" 按钮查看在线实例
相关推荐
© 2020 asciim码
人生就是一场修行