• 微信小程序点击按钮将图片保存到手机


     SaveCard: function(e) {
        let that = this;
        console.log('保存');
        var imgSrc = e.currentTarget.dataset.img;
        //获取相册授权
        wx.getSetting({
          success(res) {
            if (!res.authSetting['scope.writePhotosAlbum']) {
              wx.authorize({
                scope: 'scope.writePhotosAlbum',
                success() {
                  console.log('授权成功');
                  that.img(imgSrc)
                }
              })
            }else{
              that.img(imgSrc)
            }
          }
        })
      },
    img: function (imgSrc){
        var imgSrc = imgSrc;
        wx.downloadFile({
          url: imgSrc,
          success: function (res) {
            console.log(res); //图片保存到本地
            wx.saveImageToPhotosAlbum({
              filePath: res.tempFilePath,
              success: function (data) {
                console.log(data);
                wx.showToast({
                  title: '保存成功',
                  duration: 2000
                })
              },
              fail: function (err) {
                console.log(err);
                if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
                  wx.openSetting({
                    success(settingdata) {
                      console.log(settingdata)
                      if (settingdata.authSetting['scope.writePhotosAlbum']) {
                        wx.showToast({
                          title: '图片已保存',
                          icon:'none',
                          duration:2000
                        })
                        console.log('获取权限成功,给出再次点击图片保存到相册的提示。')
                      } else {
                        console.log('获取权限失败,给出不给权限就无法正常使用的提示')
                      }
                    }
                  })
                }
              }
            })
          }
        })
    
      },

    先授权,再保存

  • 相关阅读:
    Python基础
    pip install psycopg2出现python setup.py egg_info failed with error code 1 in /tmp/pip-build-YtLeN3/psycopg2错误处理
    Python基础
    C语言基础
    benchmarks
    用 MuGo 搭建 Go Engine 在 KGS 对战
    GPU
    linux 杀掉僵尸进程 (zombie process, defunct)
    CMakeLists.txt 语法
    软件列表(按字母排序)
  • 原文地址:https://www.cnblogs.com/caoyuna/p/11724511.html
Copyright © 2020-2023  润新知