uniapp 图片缓存

//======================
	//      图片缓存
	//======================
	function image_cache(image_url,image_name,call_back) {
		
		uni.getStorage({
			key: image_name,
			success:function(res){
				call_back(res.data)
			},
			fail:function(res){
				// 本地没有缓存 需要下载 
				uni.downloadFile({
					url: image_url,
					success: (res) => {
						if (res.statusCode === 200) {
							call_back(res.data)
							uni.setStorage({
								key:image_name,
								data:res,
								success:function(){
									
								}
							})
						}
					}
				}); 
			}
		})
	}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UniApp中可以通过uni.getImageInfo接口获取图片信息,包括图片的宽高、路径和大小等信息。可以使用uni.downloadFile接口将图片下载到本地,然后使用uni.getImageInfo获取图片信息,最后将图片路径存储到本地缓存中。 为了实现每天更换一次的效果,可以使用定时器或者计时器来触发更新操作。在更新前可以先检查本地缓存中是否存在当天的图片路径,如果存在则直接使用,否则重新下载并缓存图片。 具体实现可以参考以下步骤: 1.在App.vue中添加定时器,在每天的固定时间点触发更新操作。 ```javascript mounted() { setInterval(() => { this.updateImage(); }, 24 * 60 * 60 * 1000); // 每天的固定时间点 }, methods: { updateImage() { // 检查本地缓存中是否存在当天的图片路径,存在则直接使用,否则重新下载并缓存图片 // ... } } ``` 2.使用uni.downloadFile下载图片到本地,并使用uni.getImageInfo获取图片信息。 ```javascript uni.downloadFile({ url: imageUrl, success: res => { if (res.statusCode === 200) { uni.getImageInfo({ src: res.tempFilePath, success: info => { // 将图片路径存储到本地缓存中 // ... } }) } } }) ``` 3.在组件中使用本地缓存中的图片路径进行渲染。 ```html <template> <image :src="imagePath"></image> </template> <script> export default { data() { return { imagePath: '' } }, mounted() { // 从本地缓存中获取当天的图片路径 // ... this.imagePath = imagePath; } } </script> ``` 注意:本地缓存的数据可以使用uni.setStorageSync和uni.getStorageSync进行存储和获取。同时,为了避免缓存过多,可以设置缓存的过期时间,使用uni.removeStorageSync清除过期的缓存数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值