Skip to content

Repository files navigation

React Native Image Cache on File System with Progressive Loading

npm versionnpmLicense: MITGitHub package.json versionPlatform - Android and iOS

Inspired by:

Features

  • Cache remote images in file system with progressive loading
  • Uses react-native-file-access for file system access
  • Written in Typescript

Preview

iOS:

@georstat:react-native-image-cache_iOS_demo

Android:

@georstat:react-native-image-cache_Android_demo

Installation

yarn:

yarn add @georstat/react-native-image-cache

npm:

npm i @georstat/react-native-image-cache

Usage

Put this Global config on your app entry eg. App.tsx or index.js (Required):

import{CacheManager}from'@georstat/react-native-image-cache';import{Dirs}from'react-native-file-access';CacheManager.config={baseDir: `${Dirs.CacheDir}/images_cache/`,blurRadius: 15,sourceAnimationDuration: 1000,thumbnailAnimationDuration: 1000,};

Directory constants, choose wisely: (react-native-file-access docs)

  • Dirs.CacheDir
  • Dirs.DatabaseDir (Android only)
  • Dirs.DocumentDir
  • Dirs.LibraryDir (iOS only)
  • Dirs.MainBundleDir
  • Dirs.SDCardDir (Android only)
    • Prefer FileSystem.cpExternal() when possible.

Component:

import{CachedImage}from'@georstat/react-native-image-cache';<CachedImagesource="https://via.placeholder.com/3500x3500"style={{height: 350,width: 150}}thumbnailSource="https://via.placeholder.com/350x150"/>;

Clear local cache:

import{CacheManager}from'@georstat/react-native-image-cache';awaitCacheManager.clearCache();

Remove single cache entry:

consturi='https://.../example.jpg';awaitCacheManager.removeCacheEntry(uri);

Get local cache size:

awaitCacheManager.getCacheSize();

Props

CachedImage accepts the following props:

PropertiesPropTypeDescription
sourceString(Required) Uri of remote image.
sourceAnimationDurationNumbersource image animation duration when loading, defaults to 1000ms (overrides config)
thumbnailSourceString(Required) Uri of the thumbnail image
thumbnailAnimationDurationNumberAnimation duration for thumbnail, defaults to 1000ms (overrides config)
blurRadiusNumberAmount of blur to apply on thumbnailSource image , defaults to 15 (overrides config)
loadingImageComponentReact.NodeDefaults to an image with the loadingSource prop
loadingImageStyleObjectStyle for loading image component. Works if you don't provide a loadingImageComponent
loadingSourceobjectSource for loading Image component. Works if you don't provide loadingImageComponent
onErrorFuncRuns when there is an error loading the image from cache
resizeModeStringReact native Image component resizeMode defaults to contain
styleObjectsource AND thumbnailSource image style
optionsObjectcustom options for the fetch image http request eg. {headers:{}, body:{}}

Todo:

  • Convert library to React Hooks
  • Make baseDir configurable
  • Delete single cache entry

Authors:

About

React-Native image caching in file system with progressive loading for iOS and Android

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages