uniapp小程序下载缓存服务器上的图片

  1. 使用uni.downloadFile,但是注意下载图片的地址里的域名,需要在微信公众平台里面的downloadFile合法域名进行配置。
javascript 复制代码
export default function downloadAndCacheImage(imageUrl, name) {
	return new Promise((resolve, reject) => {
		console.log("imageUrl", imageUrl, name)
		// 检查本地缓存的更新时间
		let lastUpdateTime = uni.getStorageSync('lastUpdateTime');
		let currentDate = new Date().getTime();
		// if (!lastUpdateTime || currentDate - lastUpdateTime >= 24 * 60 * 60 * 1000) {
			// 下载最新的背景图片到本地
			uni.downloadFile({
				url: imageUrl,
				success: (res) => {
					uni.saveFile({
						tempFilePath: res.tempFilePath,
						success: (saveRes) => {
							// 保存图片路径到本地缓存
							uni.setStorageSync(name, saveRes.savedFilePath);
							uni.setStorageSync('lastUpdateTime', currentDate);
							console.log("缓存成功", res)
						}
					});
				},
				fail: (res) => {
					console.log("缓存失败", res)
				}
			});

		// }
	});

}

// 缓存图片
downloadAndCacheImage('https://www.com.jpg', 'search_page1')
this.list[2].img=uni.getStorageSync('search_page1') 
相关推荐
dreams_dream16 小时前
微信小程序:页面事件-上拉触底
微信小程序·小程序
头发还在的女程序员1 天前
基于 Spring Boot + Vue 3 + uni-app 的灵活用工系统:四端架构与业务流程设计
vue.js·spring boot·uni-app·灵活用工·灵工系统
wafrdggh2 天前
2026电商快递批量查询怎么用?固乔助手模板设置评测
大数据·数据库·小程序
wafrdggh2 天前
2026 商城小程序平台哪个价格合理?把跨境附加成本这个坑算透
大数据·小程序
Dumbness3 天前
订阅消息弹窗必须在用户点击里调:联调实验室里 timing 和 Mock 支付通知踩的坑
javascript·uni-app
码间留白4 天前
uni-app 鸿蒙离线打包:一个改动从源码到真机要穿过几条链
uni-app·鸿蒙·deveco·离线打包·uts插件
恩创软件开发4 天前
自助KTV系统管理员续单功能
经验分享·小程序
多客:duokelijie5 天前
代练订单系统选型思路:跨品类游戏接入、订单分层管理、自动结算逻辑拆解!
数据库·mysql·微信小程序·前端框架·uni-app·php·持续部署
码间留白5 天前
鸿蒙端对接 BLE 血压计:从一次握手到读数入库(二)设备篇
物联网·uni-app·鸿蒙·蓝牙ble·血压计
AI源码6 天前
在线考试小程序
小程序·notepad++