• 保存图片到本地(分享海报功能)


    
    
    <!-- 保存海报 -->
    <view class="hb_content" wx:if="{{showPop}}">
      <view class="img_con">
        <image src="{{poster_img}}"></image>
      </view>
      <button bindtap="savePoster" class="btn_saveImg">保存图片</button>
    </view>
    // 保存海报   因为需要保存到本地 所以需要获取用户的授权状态
      savePoster: function () {
        var that = this;
        wx.getSetting({
          success: res => {
            console.log(res, '获取授权状态')
            if (res.authSetting['scope.writePhotosAlbum'] == false) {   // 如果用户拒绝了授权
              // console.log('123')
              wx.showModal({      // 则提示用户打开弹窗 打开授权设置 引导用户去授权
                title: '提示',
                content: '是否授权将海报保存到相册?',
                confirmColor: '#2ca2ed',
                success: res => {
                  //点击确定打开授权设置
                  if (res.confirm) {
                    wx.openSetting({
                      success: res => {
                        setTimeout(() => {
                          if (res.authSetting['scope.writePhotosAlbum'] == true) {} else {
                            wx.showToast({
                              title: '保存失败!',
                              icon: 'none',
                              mask: true
                            })
                          }
                        }, 500)
                      }
                    })
                  }
                }
              })
            } else {  // 如果接受了先把图片下载到本地
              wx.downloadFile({
                url: that.data.poster_img, // 接口返回的地址
                success: function (res) {
                  // console.log(res, '把图片下载到本地')
                  if (res.statusCode === 200) {
                    var productSrc = res.tempFilePath;
                    that.saveImage(productSrc);  // 在这去真正的保存图片
                  } else {
                    wx.showToast({
                      title: '产品图片下载失败!',
                      icon: 'none',
                      duration: 2000,
                      success: function () {
                        var productSrc = "";
                        // that.getQrCode(productSrc);
                      }
                    })
                  }
                },
                fail: function (res) {
                  wx.showToast({
                    title: '下载失败1221' + that.data.poster,
                    icon: 'none'
                  })
                }
              })
    
            }
          }
        })
      },
      saveImage(productSrc) {  // 最后保存图片成功的状态
        let that = this
        wx.saveImageToPhotosAlbum({
          filePath: productSrc,
          success(res) {
            // console.log(res, '6666666')
            wx.showModal({
              content: '图片已保存到相册,赶紧晒一下吧~',
              showCancel: false,
              confirmText: '好的',
              confirmColor: '#333',
              success: function (res) {
                // that.closePoste();
                if (res.confirm) {
                  that.setData({
                    showPop: false
                  })
                }
              },
              fail: function (res) {
                // console.log(res)
              }
            })
          }
        })
      },
  • 相关阅读:
    FPGrowth 实现
    数据库事务的4个特性ACID
    redis简介(keeper实时报表的基本部分)
    HADOOP中的CRC数据校验文件
    潜在语义分析
    AdaBoost 算法
    cocosBuilder生成cbbi文件,绑定到cocos2d-x
    C++继承模型
    Android应用经典主界面框架之中的一个:仿QQ (使用Fragment, 附源代码)
    从今天開始学习iOS开发(iOS 7版)--实现一款App之Foundation框架的使用
  • 原文地址:https://www.cnblogs.com/xiaoxiaoxun/p/12427737.html
Copyright © 2020-2023  润新知