(转)微信小程序保存图片到相册
這篇文章主要為大家詳細介紹了微信小程序保存圖片到相冊權(quán)限設(shè)置,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
?
微信小程序保存圖片到相冊可以分成兩種:一種是保存網(wǎng)絡圖片,一種是保存本地圖片
保存網(wǎng)絡圖片:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | var url = '網(wǎng)絡圖片路徑'; wx.downloadFile({ ?url: url, ??success: function (res) { ??var benUrl = res.tempFilePath; ??//圖片保存到本地相冊 ??wx.saveImageToPhotosAlbum({ ???filePath: benUrl, ???//授權(quán)成功,保存圖片 ???success: function (data) { ???wx.showToast({ ????title: '保存成功', ????icon: 'success', ????duration: 2000 ???}) ???}, ???//授權(quán)失敗 ???fail: function (err) { ???if (err.errMsg) {//重新授權(quán)彈框確認 ????wx.showModal({ ?????title: '提示', ?????content: '您好,請先授權(quán),在保存此圖片。', ?????showCancel: false, ?????success(res) { ?????if (res.confirm) {//重新授權(quán)彈框用戶點擊了確定 ??????wx.openSetting({//進入小程序授權(quán)設(shè)置頁面 ??????success(settingdata) { ???????console.log(settingdata) ???????if (settingdata.authSetting['scope.writePhotosAlbum']) {//用戶打開了保存圖片授權(quán)開關(guān) ???????wx.saveImageToPhotosAlbum({ ????????filePath: benUrl, ????????success: function (data) { ????????wx.showToast({ ?????????title: '保存成功', ?????????icon: 'success', ?????????duration: 2000 ????????}) ????????}, ???????}) ???????} else {//用戶未打開保存圖片到相冊的授權(quán)開關(guān) ???????wx.showModal({ ????????title: '溫馨提示', ????????content: '授權(quán)失敗,請稍后重新獲取', ????????showCancel: false, ???????}) ???????} ??????} ??????}) ?????} ?????} ????}) ???} ???} ??}) ??} ?}) |
保存本地圖片:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | wx.chooseImage({ count: 1,// 默認9 ??sizeType: ['original', 'compressed'],// 可以指定是原圖還是壓縮圖,默認二者都有 ??sourceType: ['album', 'camera'],// 可以指定來源是相冊還是相機,默認二者都有 ??success: function (res) { ??// 返回選定照片的本地文件路徑列表,tempFilePath可以作為img標簽的src屬性顯示圖片 ??console.log("choose image") ??console.log(res) ??var tempFilePath = res.tempFilePaths[0] ??wx.getImageInfo({ ???src: tempFilePath, ???success: function (res) { ???console.log("get image info") ???console.log(res) ???wx.saveImageToPhotosAlbum({ ????filePath: res.path, ????success(res) { ????console.log("保存圖片成功") ????console.log(res) ????wx.showToast({ ?????title: '保存成功', ?????icon: 'success', ?????duration: 2000 ????}) ????}, ????fail(err) { ????console.log('失敗') ????console.log(err) ?? ????if (err.errMsg == "saveImageToPhotosAlbum:fail cancel") { ?????wx.openSetting({ ?????success(settingdata) { ??????console.log(settingdata) ??????if (settingdata.authSetting["scope.writePhotosAlbum"]) { ??????console.log('獲取權(quán)限成功,給出再次點擊圖片保存到相冊的提示。') ??????} else { ??????console.log('獲取權(quán)限失敗,給出不給權(quán)限就無法正常使用的提示') ??????} ?????} ?????}) ????} ????} ???}) ???} ??}) ??} }) |
?
?
轉(zhuǎn)自:https://www.jb51.net/article/184397.htm?
總結(jié)
以上是生活随笔為你收集整理的(转)微信小程序保存图片到相册的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一文详解自动驾驶V2X车联网技术丨曼孚科
- 下一篇: 【渝粤题库】广东开放大学 建筑力学与结构