在文件系统中以渐进加载方式重现原生图像缓存
灵感来自:
- wcandillon/ react-native-expo-image-cache (英语) (威廉·坎迪伦)
- WrathChaos/反应-本土-进步-快速图像 (Freaky Coder) (英语)
特征
- 缓存 远程 文件系统中带有渐进加载的图像
- 可用于本地图像
- 用途
react-native-file-access文件系统访问 - 写入
Typescript - 用途
react-native-reanimated用于动画 - 网络故障重试
- 支持新建筑,两者均
react-native-file-access财务报告和审定财务报表react-native-reanimated装潢准备好了.
注意:
如果你没有用 react-native-reanimated 2.x.x版本然后使用本模块的1.x.x版本
如果你要找一个有缩略图的图像库 @georstat/ react-native-image-gallery (英语)
预览
iOS: (英语)

机器人:

自定义装入图像组件 : (参见 实例(中文(简体) )

安装
线条 :
yarn add @georstat/react-native-image-cache react-native-file-access react-native-reanimated
无 :
npm i @georstat/react-native-image-cache react-native-file-access react-native-reanimated
链接本地软件包 :
cd ios
pod install
记着!
用于原生反应 0.65 使用 react-native-file-access >= 2.0.0
使用量
把这个全局配置放到您的应用程序条目 eg 上 . App.tsx 或者说 index.js (单位:千美元)需求:
说明数字 : retryDelay 财务报告和审定财务报表 maxRetries 是可选的,它们默认为 0 这意味着重试逻辑是 已禁用
import { CacheManager } from '@georstat/react-native-image-cache';
import { Dirs } from 'react-native-file-access';
CacheManager.config = {
baseDir: `${Dirs.CacheDir}/images_cache/`,
blurRadius: 15,
cacheLimit: 0,
maxRetries: 3 /* optional, if not provided defaults to 0 */,
retryDelay: 3000 /* in milliseconds, optional, if not provided defaults to 0 */,
sourceAnimationDuration: 1000,
thumbnailAnimationDuration: 1000,
};
自定义缓存密钥, 用于缓存来自 Amazon S3 和类似服务的图像, 请添加此( 或任何适合您需要的) CacheManager.config数字 :
CacheManager.config = {
// ...
getCustomCacheKey: (source: string) => {
// Remove params from the URL for caching images (useful for caching images from Amazons S3 bucket and etc)
let newCacheKey = source;
if (source.includes('?')) {
newCacheKey = source.substring(0, source.lastIndexOf('?'));
}
return newCacheKey;
},
};
cacheLimit 配置 : (自动搜索缓存文件)
若为 cacheLimit 设置为 0 (默认值) 那么缓存永远不会被自动浏览 。 此设置接受多个字节 eg . 1024 * 1024 * 256(~256MB)和要求 react-native-file-access 2.4.0,如果使用 < 2.4.0,则留下默认值 0 (残疾).
缓存缓存流 :
- 从中获取所有文件
baseDir。 。 。 . - 按顺序排列
lastModified,最古老的第一. - 使用
reduce数组方法. - 从第3步检查总大小并减去
cacheLimit数值. - 运行一段时间循环并不断删除文件, 只要当前缓存大小( 第 4 步) 大于触发缓存搜索所需的大小( C)
cacheLimit数值). - 上面所有步骤只有在图像尚未被缓存时才会运行,它会在下载到缓存后运行.
Pruning在iOS模拟器上以5.7MB图像和从缓存上取出~5.00副本作为基准,没有任何问题. 请注意,各装置的螺旋速度/性能可能有所不同。 使用 cacheLimit 明智和不设大值.
如果您想在模拟器上运行自己的测试,那么缓存的图像就存储在这个位置上的Mac上:
/Users/<your_name>/Library/Developer/CoreSimulator/Devices/<simulator_device_id>/ data/Containers/Data/Application/<application_id>/Library/Caches/images_cache, (中文(简体) ),
只要复制和粘贴 多个图像在那里, 有没有必要下载 通过应用程序.
目录常数, 明智选择 : (反应-内存-文件-访问文件(中文(简体) )
Dirs.CacheDirDirs.DatabaseDir(仅限机器人)Dirs.DocumentDirDirs.LibraryDir(仅iOS)Dirs.MainBundleDirDirs.SDCardDir(仅限机器人)- 偏爱
FileSystem.cpExternal()可能的话.
- 偏爱
组件 :
import { CachedImage } from '@georstat/react-native-image-cache';
<CachedImage
source="https://via.placeholder.com/3500x3500"
style={{ height: 350, width: 150 }}
thumbnailSource="https://via.placeholder.com/350x150"
/>;
前置图像并保存在缓存中 :
接受2个参数:
| 参数 | 类型 | 说明 |
|---|---|---|
image | Array or String | (需要) 远程图像的尿液或远程尿液串阵列 |
options | Object | 获取图像的自定义选项 http request eg. {headers:{}, body:{}} |
import { CacheManager } from '@georstat/react-native-image-cache';
const url = 'https://..../image.jpg';
const urls = [
'https://..../image.jpg',
'https://..../image2.jpg',
'https://..../image3.jpg',
];
CacheManager.prefetch(url); // prefetch single image
CacheManager.prefetch(urls); // prefetch mutliple images
前置图像并返回 blob/ base64 :
接受2个参数:
| 参数 | 类型 | 说明 |
|---|---|---|
image | String | (需要) 远程图像的 uri |
options | Object | 获取图像的自定义选项 http request eg. {headers:{}, body:{}} |
import { CacheManager } from '@georstat/react-native-image-cache';
const url = 'https://..../image.jpg';
CacheManager.prefetchBlob(url).then(response => console.log(response)); // response is the base64 string
清除本地缓存 :
import { CacheManager } from '@georstat/react-native-image-cache';
await CacheManager.clearCache();
删除单个缓存项 :
const uri = 'https://.../example.jpg';
await CacheManager.removeCacheEntry(uri);
获取本地缓存大小 :
await CacheManager.getCacheSize();
检查图像是否被缓存 :
await CacheManager.isImageCached(uri);
道具
CachedImage 接受下列道具:
| 属性 | 螺旋管 | 说明 |
|---|---|---|
source | String | (单位:千美元)需求(原始内容存档于2018-10-21). Uri of image. |
sourceAnimationDuration | Number | source 装入时图像动画持续时间, 默认为 1000ms( 覆盖配置) |
thumbnailSource | String | (单位:千美元)需求缩略图图像的 Uri |
thumbnailAnimationDuration | Number | 缩略图的动画持续时间, 默认为 1000ms( 覆盖配置) |
blurRadius | Number | 模糊度可应用于 thumbnailSource 图像,默认为 15 (覆盖配置) |
loadingImageComponent | React.CompnentType | 默认为图像 loadingSource 道具 |
noCache | Boolean | 不缓存图像, 默认为 false 这意味着总是缓存图像 |
maxAge | Number | 缓存图像的最大时长, 默认为 undefined (无限入棺. 自动浏览不会考虑这个值, 如果图像 cacheLimit 到达 |
loadingImageStyle | Object | 装入图像组件的样式 。 工作,如果你不提供 loadingImageComponent |
imageStyle | Object | 图像样式,在通过本地图像加载时使用 require() |
loadingSource | object | 装入图像组件的来源 。 工作,如果你不提供 loadingImageComponent |
onError | Func | 从缓存中装入图像时运行 |
onLoad | Func | 装入成功时已启动 |
onLoadEnd | Func | 装入成功或失败时已启动 |
resizeMode | String | 反应本地图像组件 调整模式大小 默认为 contain |
testID | String | 测试ID,用于测试 |
tintColor | String | 源图像的颜色 |
style | Object | source 和 thumbnailSource 图像样式 |
options | Object | 获取图像的自定义选项 http request eg. {headers:{}, body:{}} |
accessibilityHint | string | 访问提示 source (可选) |
accessibilityLabel | string | 访问标签 source (可选) |
accessibilityRole | string | 无障碍角色 source (可选,默认为) image(中文(简体) ) |
accessibilityHintThumbnail | string | 访问提示 thumbnailSource (可选) |
accessibilityLabelThumbnail | string | 访问标签 thumbnailSource (可选) |
accessibilityRoleThumbnail | string | 无障碍角色 thumbnailSource (可选,默认为) image(中文(简体) ) |
accessibilityHintLoadingImage | string | 访问提示 loadingSource (可选) |
accessibilityLabelLoadingImage | string | 访问标签 loadingSource (可选) |
accessibilityRoleLoadingImage | string | 无障碍角色 loadingSource (可选,默认为) image(中文(简体) ) |
更多关于反应土著无障碍的信息
关于反应土著无障碍的博客文章
待办事宜:
- 将库转换为反应钩
- 制作
baseDir可配置 - 删除单个缓存条目
发起人
此库为免费开发.
