微信小程序 保存图片
注: 此处使用的是小程序 wepy框架, 原生或其他的请注意转换写法
<div class="handle"> <button class="btn" open-type="{{saveset}}" @tap="bindSave"> <img src="@/images/icon_download_white.png" alt="" mode="widthFix" />保存二维码 </button> <button class="btn" open-type="share"><img src="@/images/icon_share_white.png" alt="" mode="widthFix" />分享给好友</button> </div>
// 保存图片 bindSave(e) { console.log(e) let that = this; wx.getSetting({//检查小程序的授权状态 success(res) { console.log(res)//授权后的回调 if (!res.authSetting['scope.writePhotosAlbum']) { wx.authorize({//调用 wx.authorize 弹出对话窗提醒用户是否要授权小程序得到他的授权 scope: 'scope.writePhotosAlbum', success(t) {//这里是用户同意授权后的回调 console.log("这里是用户同意授权后的回调", t) that.saveset = ""; that.getImage(); }, fail(err) {//这里是用户拒绝授权后的回调 console.log("这里是用户拒绝授权后的回调", err); that.saveset = "openSetting"; if (err.errMsg == "authorize:fail:auth deny") {//如果用户拒绝打开授权接口,通过button按钮的open-type="openSetting"属性来调起授权窗口 console.log("打开设置窗口"); if (that.data.saveset == "openSetting"){ console.log("第二次进来",that.data.opensett); wx.openSetting({ success(settingdata) { console.log(settingdata) if (settingdata.authSetting["scope.writePhotosAlbum"]) { console.log("获取权限成功,再次点击图片保存到相册") that.setData({ saveset: "" }); } else { console.log("获取权限失败") } } }); } } } }) } else {//用户已经授权过了 console.log("用户已经授权过了") that.saveset = ""; that.getImage(); } } }) }, getImage(){ wx.downloadFile({ url: 'https://xcx.rejoint.com.cn/uploads/avatar/201908/29/BwCOi6fRPzsD6ZX5LstG7FPdwqHC9u8UOH90UCew.png', //测试图片 success: function (res) { console.log(res); //图片保存到本地 wx.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success: function (data) { console.log("保存到相册", data); wx.showToast({ title: '保存成功', icon: "none" }) }, fail: function (err) { console.log(err) } }) } }) }