zoukankan      html  css  js  c++  java
  • 保存图片到本地(分享海报功能)

    
    
    <!-- 保存海报 -->
    <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)
              }
            })
          }
        })
      },
  • 相关阅读:
    hdu4280 Island Transport(最大流Dinic数组模拟邻接连边)
    hihoCoder1378 (最大流最小割)
    单聊语音
    Mybatis批量更新数据
    mysql 之 MRR
    Intellij IDEA 快捷键整理
    SpringBoot 整合 Swagger2 使用教程
    jdk/dubbo spi
    redis问题(待解决)
    JVM调优心得
  • 原文地址:https://www.cnblogs.com/xiaoxiaoxun/p/12427737.html
Copyright © 2011-2022 走看看