微信小程序保存图片功能实现
发表时间:2020-9-27
发布人:葵宇科技
浏览次数:63
功能逻辑:
先检查用户请求过的权限中是否允许"保存到相册"权限,如果没有请求过这个权限,应该向用户发起授权请求(弹窗授权),如果请求过这个权限,并且授权了,那就保存图片,显示保存成功;如果请求过,但是是拒绝的,就跳到设置页,重新授权。
<view bindtap='save'>保存图片到相册</view>
//点击保存图片
save () {
let that = this
//若二维码未加载完毕,加个动画提高用户体验
wx.showToast({
icon: 'loading',
title: '正在保存图片',
duration: 1000
})
//判断用户是否授权"保存到相册"
wx.getSetting({
success (res) {
//没有权限,发起授权
if (!res.authSetting['scope.writePhotosAlbum']) {
wx.authorize({
scope: 'scope.writePhotosAlbum',
success () {//用户允许授权,保存图片到相册
that.savePhoto();
},
fail () {//用户点击拒绝授权,跳转到设置页,引导用户授权
wx.openSetting({
success () {
wx.authorize({
scope: 'scope.writePhotosAlbum',
success() {
that.savePhoto();
}
})
}
})
}
})
} else {//用户已授权,保存到相册
that.savePhoto()
}
}
})
},
//保存图片到相册,提示保存成功
savePhoto() {
let that = this
wx.downloadFile({
url: that.data.imgUrl,
success: function (res) {
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success(res) {
wx.showToast({
title: '保存成功',
icon: "success",
duration: 1000
})
}
})
}
})
}