微信小程序下載圖片保存到本地

<image src="{{image}}" class="slide-image" mode="widthFix" bindload='imgHeight'
 data-url="{{image}}" bindlongpress="saveToPhone"/>
saveToPhone(e){
	    let imgSrc =  e.currentTarget.dataset.url;  //要保存的圖片url
	    console.log(imgSrc)
	    wx.showLoading({
	      title: '保存中...'
	    })
	    wx.downloadFile({    //下載文件資源到本地
	      url: imgSrc,
	      success: function (res) {
	        console.log(res);
	        //圖片保存到本地
	        wx.saveImageToPhotosAlbum({
	          filePath: res.tempFilePath,
	          success: function (data) {
	            wx.hideLoading()
	            wx.showToast({
	              title: '保存成功',
	              icon: 'success',
	              duration: 2000
	            })
	          },
	          fail: function (err) {
	            if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny" || err.errMsg === "saveImageToPhotosAlbum:fail:auth denied") {
	              console.log("當初用戶拒絕,再次發起授權")
	              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) {
	            console.log(res);
	            wx.hideLoading()
	          }
	        })
	      }  
	    })
	},

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章