
Security News
Feross on TBPN: How North Korea Hijacked Axios
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.
react-native-image-cached
Advanced tools
Inspired by:
react-native-file-access for file system accessTypescriptreact-native-reanimated for animationsreact-native-file-access and react-native-reanimated are Fabric ready.react-native-reanimated version 2.x.x then use version 1.x.x of this module


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
For react native >= 0.65 use react-native-file-access >= 2.0.0
App.tsx or index.js (Required):Note: retryDelay and maxRetries are optional, they default to 0 which means retry logic is disabled
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,
};
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 config: (auto pruning of cached files)If cacheLimit is set to 0 (default value) then the cache will never be auto pruned. This setting accepts a number of Bytes eg. 1024 * 1024 * 256(~256MB) and requires react-native-file-access >= 2.4.0, if you're using < 2.4.0 then leave the default value 0 (disabled).
Cache pruning flow:
baseDir.lastModified, oldest first.reduce array method.cacheLimit value.cacheLimit value).Pruning has been benchmarked on iOS simulator with a 5.7MB image and ~5.000 copies of it on cache without any issues. Please note that the pruning speed/performance might differ among devices. Use cacheLimit wisely and do not set a big value.
If you want to run your own tests on simulator then the cached images are stored in this location on a Mac:
/Users/<your_name>/Library/Developer/CoreSimulator/Devices/<simulator_device_id>/ data/Containers/Data/Application/<application_id>/Library/Caches/images_cache,
just copy and paste multiple images in there, there's no need to download them via the app.
Dirs.CacheDirDirs.DatabaseDir (Android only)Dirs.DocumentDirDirs.LibraryDir (iOS only)Dirs.MainBundleDirDirs.SDCardDir (Android only)
FileSystem.cpExternal() when possible.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"
/>;
Accepts 2 parameters:
| Parameter | Type | Description |
|---|---|---|
image | Array or String | (Required) uri of remote image or array of remote uri strings |
options | Object | (Optional) custom options for the fetch image 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
Accepts 2 parameters:
| Parameter | Type | Description |
|---|---|---|
image | String | (Required) uri of remote image |
options | Object | (Optional) custom options for the fetch image 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 accepts the following props:| Properties | PropType | Description |
|---|---|---|
source | String | (Required) Uri of image. |
sourceAnimationDuration | Number | source image animation duration when loading, defaults to 1000ms (overrides config) |
thumbnailSource | String | (Required) Uri of the thumbnail image |
thumbnailAnimationDuration | Number | Animation duration for thumbnail, defaults to 1000ms (overrides config) |
blurRadius | Number | Amount of blur to apply on thumbnailSource image , defaults to 15 (overrides config) |
loadingImageComponent | React.CompnentType | Defaults to an image with the loadingSource prop |
noCache | Boolean | Do not cache the image, defaults to false which means always cache the image |
maxAge | Number | Maximum age in hours to cache the image, defaults to undefined (infinite caching). Auto pruning won't take into consideration this value, it will delete the image anyway if the cacheLimit is reached |
loadingImageStyle | Object | Style for loading image component. Works if you don't provide a loadingImageComponent |
imageStyle | Object | Image style, use it when loading local images via require() |
loadingSource | object | Source for loading Image component. Works if you don't provide loadingImageComponent |
onError | Func | Runs when there is an error loading the image from cache |
onLoad | Func | Invoked when load completes successfully |
onLoadEnd | Func | Invoked when load either succeeds or fails |
resizeMode | String | React native Image component resizeMode defaults to contain |
testID | String | testID, useful for tests |
tintColor | String | tintColor of the source image |
style | Object | source AND thumbnailSource image style |
options | Object | custom options for the fetch image http request eg. {headers:{}, body:{}} |
accessibilityHint | string | accessibility hint for source (optional) |
accessibilityLabel | string | accessibility label for source (optional) |
accessibilityRole | string | accessibility role for source (optional, defaults to image) |
accessibilityHintThumbnail | string | accessibility hint for thumbnailSource (optional) |
accessibilityLabelThumbnail | string | accessibility label for thumbnailSource (optional) |
accessibilityRoleThumbnail | string | accessibility role for thumbnailSource (optional, defaults to image) |
accessibilityHintLoadingImage | string | accessibility hint for loadingSource (optional) |
accessibilityLabelLoadingImage | string | accessibility label for loadingSource (optional) |
accessibilityRoleLoadingImage | string | accessibility role for loadingSource (optional, defaults to image) |
baseDir configurableThis library is developed for free.
Buy us a coffee using this link: 
FAQs
React Native image file system caching for iOS and Android
We found that react-native-image-cached demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.

Security News
OpenSSF has issued a high-severity advisory warning open source developers of an active Slack-based campaign using impersonation to deliver malware.

Research
/Security News
Malicious packages published to npm, PyPI, Go Modules, crates.io, and Packagist impersonate developer tooling to fetch staged malware, steal credentials and wallets, and enable remote access.