2.1.5 • Published 2 years ago

@roman.sytnyk/react-native-image-cache-dynamic-link v2.1.5

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

React Native Image Cache on File System with Progressive Loading with Dynamic Links (Amazons S3 and etc.)

npm version npm downloads npm License: MIT GitHub package.json version Platform - Android and iOS

Fork of:

Inspired by:

Features

A fork of @georstat/react-native-image-cache with a usage of a link without params as a caching key! Could be useful for caching images from Amazon S3 and similar sources.

Caution:

If you're not using react-native-reanimated version 2.x.x then use version 1.x.x of this module

If you're looking for an image library with thumbnails visit @georstat/react-native-image-gallery

Preview

iOS:

@georstat:react-native-image-cache_iOS_demo

Android:

@georstat:react-native-image-cache_Android_demo

Custom Loading Image Component: (see example)

@georstat:react-native-image-cache_custom_loading_image_component_demo

Installation

yarn:

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

npm:

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

link native packages:

 cd ios
 pod install

**note!

For react native >= 0.65 use react-native-file-access >= 2.0.0

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,
  cacheLimit: 0,
  sourceAnimationDuration: 1000,
  thumbnailAnimationDuration: 1000,
};

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:

  1. Get all files from baseDir.
  2. Sort them by lastModified, oldest first.
  3. Get total size in Bytes by using reduce array method.
  4. Check total size from step 3 and subtract cacheLimit value.
  5. Run a while loop and keep deleting files so long as the current cache size (step 4) is larger than the size required to trigger cache pruning (cacheLimit value).
  6. All steps above will run only if the image is not already cached, it runs after downloading a new image into the cache.

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.

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';

<CachedImage
  source="https://via.placeholder.com/3500x3500"
  style={{ height: 350, width: 150 }}
  thumbnailSource="https://via.placeholder.com/350x150"
/>;

Prefetch Image(s) and store them in cache:

Accepts 2 parameters:

ParameterTypeDescription
imageArray or String(Required) uri of remote image or array of remote uri strings
optionsObject(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

Clear local cache:

import { CacheManager } from '@georstat/react-native-image-cache';

await CacheManager.clearCache();

Remove single cache entry:

const uri = 'https://.../example.jpg';

await CacheManager.removeCacheEntry(uri);

Get local cache size:

await CacheManager.getCacheSize();

Check if image is cached:

await CacheManager.isImageCached(uri);

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.CompnentTypeDefaults to an image with the loadingSource prop
noCacheBooleanDo not cache the image, defaults to false witch means always cache the image
maxAgeNumberMaximum 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
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
onLoadFuncInvoked when load completes successfully
onLoadEndFuncInvoked when load either succeeds or fails
resizeModeStringReact native Image component resizeMode defaults to contain
styleObjectsource AND thumbnailSource image style
optionsObjectcustom options for the fetch image http request eg. {headers:{}, body:{}}
accessibilityHintstringaccessibility hint for source (optional)
accessibilityLabelstringaccessibility label for source (optional)
accessibilityRolestringaccessibility role for source (optional, defaults to image)
accessibilityHintThumbnailstringaccessibility hint for thumbnailSource (optional)
accessibilityLabelThumbnailstringaccessibility label for thumbnailSource (optional)
accessibilityRoleThumbnailstringaccessibility role for thumbnailSource (optional, defaults to image)
accessibilityHintLoadingImagestringaccessibility hint for loadingSource (optional)
accessibilityLabelLoadingImagestringaccessibility label for loadingSource (optional)
accessibilityRoleLoadingImagestringaccessibility role for loadingSource (optional, defaults to image)

More info about React Native Accessibility

Blog Article about React Native Accessibility

Todo:

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

Authors:

2.1.5

2 years ago

2.1.4

2 years ago

2.1.2

2 years ago

2.1.1

2 years ago