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') 
相关推荐
spmcor1 天前
身份证读卡“无感登录”方案实践:从手动点击到自动检测
uni-app
PedroQue992 天前
uni-router v1.8.0新增冷启动守卫补执行
前端·uni-app
PedroQue993 天前
uni-router v1.7.0重磅更新:守卫重定向自由掌控
前端·uni-app
一份执念4 天前
uni-app项目 (vue+vite + uni-UI)中引入umd格式JS文件,微信小程序中导入报错处理方案
前端·uni-app·echarts
PedroQue995 天前
V1.6.1性能优化:高频路径提速与代码精简
前端·uni-app
夏碧笔7 天前
uni-app跨端地图实战:用第三方LBS替代微信平台收费服务
uni-app
m0_5261194011 天前
iconfont我修改好颜色,但是在小程序项目是黑色的
小程序
用户69903048487511 天前
try catch使用场景 处理同步代码错误兼容用的
javascript·uni-app
ITKEY_11 天前
uniapp微信开发者工具 更改AppID失败 touristappid
uni-app
2601_9567436811 天前
2026 上海小程序开发甄选:源码、云函数、跨端兼容技术评判
小程序·开发经验·上海