微信小程序 長按保存圖片

小程序沒找到長按識別二維碼的功能,只能用這種先保存圖片,再去掃一掃的形式。小程序

 

<image src="../../assets/images/list0.png" bindtouchstart='timestart' bindtouchend='timeend' bindtap='saveImg'></image>
<view>一、長按保存二維碼圖片</view>
<view>二、打開微信掃一掃,選擇相冊圖片</view>
//點擊開始的時間  
  timestart: function (e) {
      var _this = this;
      _this.setData({ timestart: e.timeStamp });
  },
  //點擊結束的時間
  timeend: function (e) {
      var _this = this;
      _this.setData({ timeend: e.timeStamp });
  },

  //保存圖片
  saveImg: function (e) {
      var _this = this;
      var times = _this.data.timeend - _this.data.timestart;
      if (times > 300) {
      console.log("長按");
      wx.getSetting({
        success: function (res) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success: function (res) {
              console.log("受權成功");
              var imgUrl = "http://shareds.oss-cn-hangzhou.aliyuncs.com/exhibit/20180815/tmp_35d425e6e732ba516f2e8c9988706eba.jpg";
              wx.downloadFile({//下載文件資源到本地,客戶端直接發起一個 HTTP GET 請求,返回文件的本地臨時路徑
                url: imgUrl,
                success: function (res) {
                  // 下載成功後再保存到本地
                  wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,//返回的臨時文件路徑,下載後的文件會存儲到一個臨時文件
                    success: function (res) {
                      wx.showToast({
                        title: '成功保存到相冊',
                        icon: 'success'
                      })
                    }
                  })
                }
              })
            }
          })
        }
      })
    }
  },
相關文章
相關標籤/搜索