本文實(shí)例講述了微信小程序實(shí)現(xiàn)的圖片保存功能。分享給大家供大家參考,具體如下:
微信小程序保存圖片分為兩步:
1.下載文件資源到本地,客戶端直接發(fā)起一個(gè) HTTP GET 請(qǐng)求,返回文件的本地臨時(shí)路徑。
即:調(diào)用函數(shù)wx.downloadFile({})
2.保存圖片到系統(tǒng)相冊(cè)。
即:調(diào)用函數(shù)wx.saveImageToPhotosAlbum({})
具體代碼如下:
.wxml
<button data-image='{{圖片路徑}}' bindtap="saveImage" >保存圖片</button>
.js
saveImage: function (e) { wx.downloadFile({ url: 服務(wù)器Http請(qǐng)求 +圖片路徑, success: function (res) { var imageFilePath = res.tempFilePath; if (!util.isNull(imageFilePath)) { wx.saveImageToPhotosAlbum({ filePath: imageFilePath, success: function (data) { wx.showToast({ title: "保存成功", }) }, fail: function (res) { wx.showToast({ title: "保存失敗", }) } }) } }, })},
關(guān)于wx.downloadFile具體說(shuō)明還可參考官方文檔:https://developers.weixin.qq.com/miniprogram/dev/api/wx.downloadFile.html
希望本文所述對(duì)大家微信小程序開發(fā)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選