小程序预加载图片,清除缓存

data:{
	imageSrc: [{
        name: 'levelOne',
        src: image + 'levelOne.gif'
      },
      {
        name: 'levelTwo',
        src: image + 'levelTwo.gif'
      },
      {
        name: 'levelThree',
        src: image + 'levelThree.gif'
      }]
},
//缓存所有图片
  imageLoad: function() {
    var that = this
    var imageSrc = this.data.imageSrc
    var x = 32 / imageSrc.length
    var newPicture = {}
    console.log("加载了")
    this.setData({
      barWidth: 0
    })
    that.getammeternumber(0, 0, imageSrc.length, newPicture)
  },
  //递归调用
  getammeternumber: function(barWidth, i, length, newPicture) {
    var that = this;
    var imageSrc = that.data.imageSrc
    var x = 32 / imageSrc.length
    wx.downloadFile({
      url: imageSrc[i].src,
      success(res) {
        if (res.statusCode === 200) {
          // wx.compressImage({
          //   src: res.tempFilePath, // 图片路径
          //   quality: 10, // 压缩质量
          //   success(res) {
          console.log("qq", res.tempFilePath)
          const fs = wx.getFileSystemManager()
          fs.saveFile({
            tempFilePath: res.tempFilePath,
            success(res) {
              console.log('图片缓存成功', res.savedFilePath)
              newPicture[imageSrc[i].name] = res.savedFilePath
              console.log("i", i, 'length', length)
              that.setData({
                barWidth: barWidth + x,
              })
              if (++i < length) {
                that.getammeternumber(that.data.barWidth, i, length, newPicture);
              } else {
                //跳转
                console.log("qqqqq", newPicture)
                wx.setStorageSync('newPicture', newPicture)
                wx.redirectTo({
                  url: './index',
                })
                that.setData({
                  begin: true
                })
              }
            },
            fail(res) {
              that.clean()
              that.checkRe()
            }
          })
          //   },
          //   fail(res) {
          //     console.log(res)
          //   }
          // })
        }
      },
      fail(res) {
        that.clean()
        that.checkRe()
      }
    })
  }
//每次选择性清除缓存
  clean: function() {
    var that = this
    wx.getSavedFileList({ // 获取文件列表
      success(res) {
        //是否有缓存
        console.log("清除缓存", res)
        that.setData({
          loadImage: true
        })
        res.fileList.forEach((val, key) => { // 遍历文件列表里的数据
          // 删除存储的垃圾数据
          wx.removeSavedFile({
            filePath: val.filePath
          });
        })
      }
    })
    try {
      wx.clearStorageSync()
    } catch (e) {
      console.log("清除1", e)
    }
    try {
      wx.clearStorageSync()
    } catch (e) {
      console.log("清除2", e)
    }
  },
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值