欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序保存多張圖片的實(shí)現(xiàn)方法

 更新時間:2019年03月05日 09:13:33   作者:乖摸摸頭  
這篇文章主要介紹了微信小程序保存多張圖片的實(shí)現(xiàn)方法,使用promise 隊列,保存多張圖片到手機(jī)相冊,小編覺得具有一定的參考價值,有興趣的可以了解一下

前言

使用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)目案例

github地址

git clone https://github.com/sunnie1992/soul-weapp.git

直接用微信小程序開發(fā)工具打開就可以看到案例了

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論