import Card from '_global/Card'; import React, { useState, useRef } from 'react'; import { View, Text, ScrollView, Platform } from 'react-native'; import { Image, Space, Button, Input } from '@xrnjs/ui'; const defaultImage = 'https://static.xtransfer.com/boss/static/wechatqr_6f9e679ec75432d7.png'; const EventsAndCache = () => { const [imageUri, setImageUri] = useState(defaultImage); const [eventLogs, setEventLogs] = useState([]); const [cachePath, setCachePath] = useState(''); const imageUriRef = useRef(defaultImage); const isWeb = Platform.OS === 'web'; const addLog = (message: string) => { if (!isWeb) { setEventLogs((prev) => [ ...prev, `${new Date().toLocaleTimeString()}: ${message}`, ]); } }; const clearLogs = () => { setEventLogs([]); }; const updateImageUri = () => { setImageUri(imageUriRef.current); clearLogs(); }; const clearCache = () => { Image.clearDiskCache?.(); Image.clearMemoryCache?.(); addLog('缓存已清除'); }; const getCachePath = async () => { try { const path = await Image.getCachePathAsync(imageUri); path && setCachePath(path); addLog(`缓存路径: ${path}`); } catch (error) { addLog(`获取缓存路径失败: ${error}`); } }; return ( 当前图片: addLog('开始加载 (onLoadStart)')} onLoad={(event) => addLog( `加载成功 (onLoad): ${event.source?.width}x${event.source?.height}` ) } onLoadEnd={() => addLog('加载结束 (onLoadEnd)')} onProgress={(event) => addLog( `加载进度 (onProgress): ${event.loaded}/${event.total}` ) } onError={(event) => addLog(`加载错误 (onError): ${event.error}`) } onDisplay={() => addLog('显示成功 (onDisplay)')} /> 图片地址: { imageUriRef.current = value; }} placeholder="输入图片URL" /> 事件日志: {eventLogs.length > 0 ? eventLogs.join('\n') : '暂无事件日志'} 缓存策略演示 无缓存 磁盘缓存 内存缓存 内存+磁盘 {cachePath ? ( 缓存路径: {cachePath} ) : null} 使用自定义缓存键 带请求头的图片 用于列表优化的回收键 ); }; export default EventsAndCache;