小程序图片image标签的src如何做缓存?
网友回复
我们需要自定义一个组件,实现根据src从本地缓冲中获取数据,没有缓冲就下载。
微信小程序提供了 wx.setStorage 和 wx.getStorage 来存储数据,以及 wx.saveFile 来保存文件。下面是一个适用于微信小程序的图片缓存组件:
<!-- components/cached-image/cached-image.wxml --> <image src="{{imageSrc}}" mode="{{mode}}" lazy-load="{{lazyLoad}}" show-menu-by-longpress="{{showMenuByLongpress}}" binderror="onImageError" bindload="onImageLoad" ></image>
// components/cached-image/cached-image.js Component({ properties: { src: { type: String, observer: function(newVal) { this.loadImage(newVal); } }, mode: { type: String, value: 'aspectFill' }, lazyLoad: { type: Boolean, value: false }, showMenuByLongpress: { type: Boolean, value: false } }, data: { imageSrc: '' }, methods: { async loadImage(url) { if (!url) return; try { // 检查缓存 const cachedPath = await this.getCachedImage(url); if (cachedPath) { this.setData({ imageSrc: cachedPath }); return; } // 下载并缓存图片 const downloadRes = await this.downloadImage(url); const savedFilePath = await this.saveFile(downloadRes.tempFilePath); ...
点击查看剩余70%
ollama中的deepseek有没有本地越狱提示词?
deepseek r1各个版本本地电脑部署运行的硬件要求是多少?
python如何分离视频中的音频与视频为mp4与wav文件?
python报错: ImportError: urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the ‘ssl‘ module is comp
ps支持命令行执行脚本文件修改图片吗?
传统的个人博客站长如何应对ai搜索的流量锐减?
centos安装python3.10提示ssl模块不存在?
deepseek r1如何使用本地gpu运行?
有没有哪款ai工具可以实现全自动生成发布运营自媒体图文视频内容为用户赚钱?
微信和抖音小程序如何下载wav和mp3音频文件保存到手机上?