溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

小程序如何保存圖片到相冊

發(fā)布時(shí)間:2022-03-15 11:01:22 來源:億速云 閱讀:692 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“小程序如何保存圖片到相冊”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

小程序如何保存圖片到相冊

官方文檔

保存圖片前需要授權(quán),因此我們先授權(quán),代碼如下:

//獲取相冊授權(quán)wx.getSetting({success(res) {if (!res.authSetting['scope.writePhotosAlbum']) {wx.authorize({scope:'scope.writePhotosAlbum',success() {console.log('授權(quán)成功')}})}}})//此方法可以寫在app.js中,也可以寫在調(diào)用保存按鈕時(shí)。

獲取授權(quán)

第一種方法,網(wǎng)絡(luò)圖片保存:

var imgSrc = "http://yijiao.oss-cn-qingdao.aliyuncs.com/images/http://tmp/wx1b4e5e756cd48af1.o6zAJsws4grEQvYrWTjBigy-6QaU.0llhudiKSF2V955a1c48350d9328ef064b4d36d12746.jpg"wx.downloadFile({url: imgSrc,success: function (res) {console.log(res);//圖片保存到本地wx.saveImageToPhotosAlbum({filePath: res.tempFilePath,success: function (data) {wx.showToast({title: '保存成功',icon: 'success',duration: 2000})},fail: function (err) {console.log(err);if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {console.log("當(dāng)初用戶拒絕,再次發(fā)起授權(quán)")wx.openSetting({success(settingdata) {console.log(settingdata)if (settingdata.authSetting['scope.writePhotosAlbum']) {console.log('獲取權(quán)限成功,給出再次點(diǎn)擊圖片保存到相冊的提示。')} else {console.log('獲取權(quán)限失敗,給出不給權(quán)限就無法正常使用的提示')}}})}},complete(res){console.log(res);}})}})

第二種方法:選擇相冊圖片存入本地

wx.chooseImage({count:1,// 默認(rèn)9sizeType: ['original','compressed'],// 可以指定是原圖還是壓縮圖,默認(rèn)二者都有sourceType: ['album','camera'],// 可以指定來源是相冊還是相機(jī),默認(rèn)二者都有success:function (res) {// 返回選定照片的本地文件路徑列表,tempFilePath可以作為img標(biāo)簽的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)限成功,給出再次點(diǎn)擊圖片保存到相冊的提示。')}else {console.log('獲取權(quán)限失敗,給出不給權(quán)限就無法正常使用的提示')}}})}}})}})}})

“小程序如何保存圖片到相冊”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI