小程序保存圖片的坑


 

saveHB: function () {
    console.log('保存海報');
    // wx.navigateTo({
    //   url: '/pages/hualang/shengchenghb/taskCardPost',
    // })
      var that =this;
      wx.showLoading({
        title: '保存中...',
      })
      wx.downloadFile({
       url: that.data.iteminfo.framed_url,
        success: function (res) {
          wx.hideLoading()
           var imageFilePath = res.tempFilePath;
              if (imageFilePath!='') {
               wx.saveImageToPhotosAlbum({
               filePath: imageFilePath,
               success: function (data) {
               wx.showToast({
               title: "保存成功",
               })
             }, 
             fail: function (err) {
              if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny" || err.errMsg ==="saveImageToPhotosAlbum:fail authorize no response") {
                // 這邊微信做過調整,必須要在按鈕中觸發,因此需要在彈框回調中進行調用
                wx.showModal({
                  title: '提示',
                  content: '需要您授權保存相冊',
                  showCancel: false,
                  success:modalSuccess=>{
                    wx.openSetting({
                      success(settingdata) {
                        console.log("settingdata", settingdata)
                        if (settingdata.authSetting['scope.writePhotosAlbum']) {
                          wx.showModal({
                            title: '提示',
                            content: '獲取權限成功,再次點擊圖片即可保存',
                            showCancel: false,
                          })
                        } else {
                          wx.showModal({
                            title: '提示',
                            content: '獲取權限失敗,將無法保存到相冊哦~',
                            showCancel: false,
                          })
                        }
                      },
                      fail(failData) {
                        console.log("failData",failData)
                      },
                      complete(finishData) {
                        console.log("finishData", finishData)
                      }
                    })
                  }
                })
              }
            },
            complete(res) {
              wx.hideLoading()
            }
       })
       }
     },
     
    })
  },

 

 上面的代碼是解決權限問題的,還有一個坑就是真機調試是好的,體驗版本保存不了,后來發現是圖片路徑的域名不合法。

 

 必須設置downloadFile合法域名


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM