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') 
相关推荐
万岳科技系统开发7 小时前
校园跑腿外卖搭建助力高校周边商家拓展线上业务
大数据·人工智能·小程序
微三云 - 廖会灵 (私域系统开发)14 小时前
消费返物业费平台搭建:美家时代商业模式拆解与系统开发选型指南
小程序·系统架构
greenbbLV16 小时前
中小公司积分商城选型:SaaS与私有化方案对比解析
大数据·前端·小程序
结网的兔子2 天前
【前端开发】Web端迁移至 uni-app 及鸿蒙扩展方案对比
前端·uni-app·harmonyos
2501_915909062 天前
iOS 应用反调试技详解术 检测调试器的原理与防护实践
android·ios·小程序·https·uni-app·iphone·webview
AI多Agent协作实战派2 天前
AI多Agent协作系统实战(二十八):顶栏统一战争——从1个页面异常到31个页面全量对齐
数据库·人工智能·uni-app
00后程序员张2 天前
iOS加固技术路线全面解析:Bitcode模式、源码模式与汇编模式对比及爱加密优势
android·汇编·ios·小程序·uni-app·cocoa·iphone
海纳百川·纳海川2 天前
租房行业数字化:换个思路解决“老问题”
大数据·微信小程序·小程序
上海云盾-小余2 天前
CC 高频请求 + DDoS 流量攻击实战防御:全链路 WAF 流量清洗部署完整流程
网络·爬虫·安全·小程序·ddos
2501_916008892 天前
iOS IPA文件反编译与打包操作方法,拆包分析防护和加固打包
android·macos·ios·小程序·uni-app·cocoa·iphone