微信小程序长按保存图片
生活随笔
收集整理的這篇文章主要介紹了
微信小程序长按保存图片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
微信小程序長按保存圖片
wxml部分
<image data-url="{{item}}" bindlongtap="longPressSaveImg " />js部分
//20200720 新增長按保存圖片longPressSaveImg (e) {let that = thislet url = e.currentTarget.dataset.url;console.log(url);if (!wx.saveImageToPhotosAlbum) {wx.showModal({title: '提示',content: '當前微信版本過低,無法使用該功能,請升級到最新微信版本后重試。'})return;}// 檢查用戶授權wx.getSetting({success(res) {if (!res.authSetting['scope.writePhotosAlbum']) {// 接口調用詢問 wx.authorize({scope: 'scope.writePhotosAlbum',success() {that.saveImg(url);},fail() {// 用戶拒絕授權 wx.showModal({title: '保存圖片',content: '保存圖片需要您授權',showCancel: true,confirmText: '確定',success (res) {if (res.confirm) {// 打開設置頁面 wx.openSetting({success (res) {if (res.authSetting['scope.writePhotosAlbum']) {that.saveImg(url);} else {wx.showToast({title:'授權失敗!',icon:"none"});}},fail (res) {wx.showToast({title:'授權失敗!',icon:"none"});}});} else if (res.cancel) {wx.showToast({title:'您已取消授權!',icon:"none"});}}})}})} else {that.saveImg(url)}},fail(res) {console.log(res);}})},saveImg (imageUrl) { wx.getImageInfo({src: imageUrl,success:(res)=> {let path = res.path;wx.saveImageToPhotosAlbum({filePath:path,success:(res)=> { wx.showToast({title:'保存成功'});},fail:(res)=>{wx.showToast({title:'您已取消保存',icon:"none"});}})},fail:(res)=> {console.log(res);}})這里下載圖片到小程序資源庫,感覺會不會占本地內存,導致小程序變卡,所以用了上邊那個方法,// wx.downloadFile({// url: imageUrl,// success (res) {// // 保存圖片到系統相冊 // wx.saveImageToPhotosAlbum({// filePath: res.tempFilePath,// success(res) {// wx.showToast({// title: '保存成功'// });// },// fail(res) {// wx.showToast({// title: '您已取消保存',// icon:'none'// });// }// })// },// fail (res) {// }// })},總結
以上是生活随笔為你收集整理的微信小程序长按保存图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信小程序 - 实现左滑动删除功能
- 下一篇: Vue中实现输入框Input输入格式限制