微信小程序保存多张图片的实现方法
更新时间:2019年03月05日 09:13:33 作者:乖摸摸头
这篇文章主要介绍了微信小程序保存多张图片的实现方法,使用promise 队列,保存多张图片到手机相册,小编觉得具有一定的参考价值,有兴趣的可以了解一下
前言
使用promise 队列,保存多张图片到手机相册
问题:有些手机会出现只能保存五张图片,报错信息:无法写入
promise需要好好学习
核心代码
// pages/saveImgs/index.js
import { writePhotosAlbum } from '../../utils/util'
Page({
/**
* 页面的初始数据
*/
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
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function(options) {},
// 下载图片
downloadImgs() {
var _this = this
// 获取保存到相册权限
writePhotosAlbum(
function success() {
wx.showLoading({
title: '加载中',
mask: true
})
// 调用保存图片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)
}
})
})
}
})

项目案例
git clone https://github.com/sunnie1992/soul-weapp.git
直接用微信小程序开发工具打开就可以看到案例了
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
TypeScript 使用 Tuple Union 声明函数重载
这篇文章主要介绍了TypeScript 使用 Tuple Union 声明函数重载,TypeScript 中为函数添加多个签名后,依然需要添加相应的代码来判断并从不同的签名参数列表中获取对应的参数,下文就来探索方法和技巧吧2022-04-04
web3.js增加eth.getRawTransactionByHash(txhash)方法步骤
这篇文章主要介绍了web3.js增加eth.getRawTransactionByHash(txhash)方法步骤,需要的朋友可以参考下2018-03-03
JavaScript如何通过userAgent判断几个常用浏览器详解
userAgent 属性是一个只读的字符串,声明了浏览器用于 HTTP 请求的用户代理头的值,这篇文章主要给大家介绍了关于JavaScript如何通过userAgent判断几个常用浏览器的相关资料,需要的朋友可以参考下2021-06-06
JS表单验证方法实例小结【电话、身份证号、Email、中文、特殊字符、身份证号等】
这篇文章主要介绍了JS表单验证方法,结合实例形式总结分析了常用的表单验证技巧,包括电话、身份证号、Email、中文、特殊字符、身份证号等验证方法,需要的朋友可以参考下2017-02-02


最新评论