微信小程序保存多張圖片的實(shí)現(xiàn)方法
前言
使用promise 隊列,保存多張圖片到手機(jī)相冊
問題:有些手機(jī)會出現(xiàn)只能保存五張圖片,報錯信息:無法寫入
promise需要好好學(xué)習(xí)
核心代碼
// pages/saveImgs/index.js import { writePhotosAlbum } from '../../utils/util' Page({ /** * 頁面的初始數(shù)據(jù) */ data: { list: [ 'https://timgs.top1buyer.com/admin/special/special_img_20190301160008479.jpg', 'https://timgs.top1buyer.com/admin/special/special_img_20190301160013201.jpg', 'https://timgs.top1buyer.com/admin/special/special_img_20190301160015969.jpg', 'https://timgs.top1buyer.com/admin/special/special_img_20190301160025498.jpg', 'https://timgs.top1buyer.com/admin/special/special_img_20190301160031519.jpg', 'https://timgs.top1buyer.com/admin/special/special_img_20190301160042689.jpg', 'https://timgs.top1buyer.com/admin/special/special_img_20190301160108243.jpg', 'https://timgs.top1buyer.com/admin/special/special_img_20190301160111756.jpg', 'https://timgs.top1buyer.com/admin/special/special_img_20190304160141454.jpg' ], loading:false }, /** * 生命周期函數(shù)--監(jiān)聽頁面加載 */ onLoad: function(options) {}, // 下載圖片 downloadImgs() { var _this = this // 獲取保存到相冊權(quán)限 writePhotosAlbum( function success() { wx.showLoading({ title: '加載中', mask: true }) // 調(diào)用保存圖片promise隊列 _this .queue(_this.data.list) .then(res => { wx.hideLoading() wx.showToast({ title: '下載完成' }) }) .catch(err => { wx.hideLoading() console.log(err) }) }, function fail() { wx.showToast({ title: '您拒絕了保存到相冊' }) } ) }, // 隊列 queue(urls) { let promise = Promise.resolve() urls.forEach((url, index) => { promise = promise.then(() => { return this.download(url) }) }) return promise }, // 下載 download(url) { return new Promise((resolve, reject) => { wx.downloadFile({ url: url, success: function(res) { var temp = res.tempFilePath wx.saveImageToPhotosAlbum({ filePath: temp, success: function(res) { resolve(res) }, fail: function(err) { reject(res) } }) }, fail: function(err) { reject(err) } }) }) } })
項(xiàng)目案例
git clone https://github.com/sunnie1992/soul-weapp.git
直接用微信小程序開發(fā)工具打開就可以看到案例了
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信小程序通過保存圖片分享到朋友圈功能
- 微信小程序?qū)崿F(xiàn)保存圖片到相冊功能
- 在微信小程序中保存網(wǎng)絡(luò)圖片
- 微信小程序?qū)崿F(xiàn)圖片放大預(yù)覽功能
- 微信小程序圖片選擇、上傳到服務(wù)器、預(yù)覽(PHP)實(shí)現(xiàn)實(shí)例
- 微信小程序?qū)崿F(xiàn)圖片上傳、刪除和預(yù)覽功能的方法
- 微信小程序上傳圖片到服務(wù)器實(shí)例代碼
- 微信小程序 動態(tài)的設(shè)置圖片的高度和寬度詳解及實(shí)例代碼
- 微信小程序-拍照或選擇圖片并上傳文件
- 微信小程序圖片選擇區(qū)域裁剪實(shí)現(xiàn)方法
- 微信小程序?qū)崿F(xiàn)的圖片保存功能示例
相關(guān)文章
JavaScript實(shí)現(xiàn)篩選數(shù)組
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)篩選數(shù)組,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-03-03微信小程序?qū)崿F(xiàn)多選框全選與取消全選功能示例
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)多選框全選與取消全選功能,結(jié)合實(shí)例形式分析了微信小程序多選框功能實(shí)現(xiàn)、布局顯示及全選、取消全選相關(guān)操作技巧,需要的朋友可以參考下2019-05-05用JS編寫一個函數(shù),返回數(shù)組中重復(fù)出現(xiàn)過的元素(實(shí)例)
下面小編就為大家?guī)硪黄肑S編寫一個函數(shù),返回數(shù)組中重復(fù)出現(xiàn)過的元素(實(shí)例)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09javascript StringBuilder類實(shí)現(xiàn)
一個簡單的StringBuilder類實(shí)現(xiàn)2008-12-12基于JavaScript實(shí)現(xiàn)簡易計算器
這篇文章主要為大家詳細(xì)介紹了基于JavaScript實(shí)現(xiàn)簡易計算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06JavaScript cookie的設(shè)置獲取刪除詳解
這篇文章主要介紹了JavaScript cookie的設(shè)置獲取刪除等等,,需要的朋友可以參考下2014-02-02