微信小程序保存照片到本地

handleSave(e) {
let imgUrl = e.currentTarget.dataset.img
wx.showLoading({
title: ‘保存中…’,
})
wx.downloadFile({
url: imgUrl, // 图片路径
success: function (res) {
if (res.statusCode === 200) {
wx.saveImageToPhotosAlbum({
title: “”,
filePath: res.tempFilePath,
success(res) {
wx.showToast({
title: ‘保存成功’,
duration: 4000,
icon: ‘success’,
})
},
fail(res) {
if (res.errMsg) { //重新授权弹框确认
wx.showModal({
title: ‘提示’,
content: ‘您好,请先授权,在保存此图片。’,
showCancel: false,
success(res) {
if (res.confirm) { //重新授权弹框用户点击了确定
wx.openSetting({ //进入小程序授权设置页面
success(settingdata) {
if (settingdata.authSetting[‘scope.writePhotosAlbum’]) { //用户打开了保存图片授权开关
wx.saveImageToPhotosAlbum({
filePath: imgUrl,
success: function (data) {
wx.showToast({
title: ‘保存成功’,
icon: ‘success’,
duration: 2000,
})
},
})
} else { //用户未打开保存图片到相册的授权开关
wx.showModal({
title: ‘温馨提示’,
content: ‘授权失败,请稍后重新获取’,
showCancel: false,
})
}
}
})
}
}
})
}
}
})
}
},
fail(err) {
//console.log(err)
}
})
},
预览图片
previewImage(e) {
const current = e.currentTarget.dataset.img //获取当前点击的 图片 url
let arr = []
arr.push(current)
console.log(arr)
wx.previewImage({
current,
urls: arr //这里可以放整个需要预览的图片的数组 this.data.imgList
})
},

猜你喜欢

转载自blog.csdn.net/weixin_43764828/article/details/127765405
今日推荐