All Projects → kfiroo → React Native Cached Image

kfiroo / React Native Cached Image

Licence: mit
CachedImage component for react-native

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Native Cached Image

Sjnetwork
SJNetwork is a high level network request tool based on AFNetworking and inspired on YTKNetwork.
Stars: ✭ 231 (-74.04%)
Mutual labels:  cache, image
Sqip
"SQIP" (pronounced \skwɪb\ like the non-magical folk of magical descent) is a SVG-based LQIP technique.
Stars: ✭ 3,074 (+245.39%)
Mutual labels:  image, images
Selene
A C++17 image representation, processing and I/O library.
Stars: ✭ 266 (-70.11%)
Mutual labels:  image, images
Ffimageloading
Image loading, caching & transforming library for Xamarin and Windows
Stars: ✭ 1,288 (+44.72%)
Mutual labels:  cache, image
Sdwebimage
Asynchronous image downloader with cache support as a UIImageView category
Stars: ✭ 23,928 (+2588.54%)
Mutual labels:  cache, image
Bbwebimage
A high performance Swift library for downloading, caching and editing web images asynchronously.
Stars: ✭ 128 (-85.62%)
Mutual labels:  cache, image
Crunch
Crunch is a tool for lossy PNG image file optimization. It combines selective bit depth, color type, and color palette reduction with zopfli DEFLATE compression algorithm encoding using the pngquant and zopflipng PNG optimization tools. This approach leads to a significant file size gain relative to lossless approaches at the expense of a relatively modest decrease in image quality (see example images below).
Stars: ✭ 3,074 (+245.39%)
Mutual labels:  image, images
Silentbox
A lightbox inspired Vue.js component.
Stars: ✭ 196 (-77.98%)
Mutual labels:  image, images
Kingfisher
A lightweight, pure-Swift library for downloading and caching images from the web.
Stars: ✭ 19,512 (+2092.36%)
Mutual labels:  cache, image
Npm Gif
Replace NPM install's progress bar with a GIF!
Stars: ✭ 332 (-62.7%)
Mutual labels:  image, images
Rximagepicker
Android图片相册预览选择器、支持AndroidX,支持图片的单选、多选、图片预览、图片文件夹切换、在选择图片时调用相机拍照
Stars: ✭ 85 (-90.45%)
Mutual labels:  cache, image
Ueberzug
ueberzug is a command line util which allows to display images in combination with X11
Stars: ✭ 711 (-20.11%)
Mutual labels:  image, images
Extended image
A powerful official extension library of image, which support placeholder(loading)/ failed state, cache network, zoom pan image, photo view, slide out page, editor(crop,rotate,flip), paint custom etc.
Stars: ✭ 1,021 (+14.72%)
Mutual labels:  cache, image
Recent Images
Do you noticed the new feature of Telegram or Instagram?! They show your latest images when you try to attach or post a picture. So I developed this library the same with lots of customization. Simple way to get all images of device based on date taken, name, id and other customization
Stars: ✭ 182 (-79.55%)
Mutual labels:  cache, image
Exiftool Vendored.js
Fast, cross-platform Node.js access to ExifTool
Stars: ✭ 200 (-77.53%)
Mutual labels:  image, images
Yii2 Imagine
Yii 2 imagine extension
Stars: ✭ 271 (-69.55%)
Mutual labels:  image, images
Uploadcare Widget
Uploadcare Widget, an ultimate tool for HTML5 file upload supporting multiple file upload, drag&drop, validation by file size/file extension/MIME file type, progress bar for file uploads, image preview.
Stars: ✭ 183 (-79.44%)
Mutual labels:  image, images
Vue Cool Lightbox
Vue.js lightbox inspired by fancybox.
Stars: ✭ 196 (-77.98%)
Mutual labels:  image, images
Easystash
🗳Easy data persistence in Swift
Stars: ✭ 303 (-65.96%)
Mutual labels:  cache, image
Oblique
With Oblique explore new styles of displaying images
Stars: ✭ 633 (-28.88%)
Mutual labels:  image, images

react-native-cached-image

CachedImage component for react-native

This package is greatly inspired by @jayesbe's amazing react-native-cacheable-image but adds some functionality that we were missing when trying to handle caching images in our react-native app.

Installation

npm install react-native-cached-image --save
- or -
yarn add react-native-cached-image

We use react-native-fetch-blob to handle file system access in this package and it requires an extra step during the installation.

You should only have to do this once.

react-native link react-native-fetch-blob

Or, if you want to add Android permissions to AndroidManifest.xml automatically, use this one:

RNFB_ANDROID_PERMISSIONS=true react-native link react-native-fetch-blob

Network Status - Android only

Add the following line to your android/app/src/AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Usage

TODO - add usage example

import React from 'react';
import {
    CachedImage,
    ImageCacheProvider
} from 'react-native-cached-image';

const images = [
    'https://example.com/images/1.jpg',
    'https://example.com/images/2.jpg',
    'https://example.com/images/3.jpg',
    // ...
];

export default class Example extends React.Component {
    render() {
        return (
            <ImageCacheProvider
                urlsToPreload={images}
                onPreloadComplete={() => console.log('hey there')}>

                <CachedImage source={{uri: images[0]}}/>

                <CachedImage source={{uri: images[1]}}/>

                <CachedImage source={{uri: images[2]}}/>

            </ImageCacheProvider>
        );
    }
}

API

This package exposes 3 modules:

const {
    CachedImage,            // react-native component that is a drop-in replacement for your react-native `Image` components
    ImageCacheProvider,     // a top level component that provides accsess to the underlying `ImageCacheManager` and preloads images
    ImageCacheManager,      // the logic behind cache machanism - ttl, fs, url resolving etc. 
} = require('react-native-cached-image');

ImageCacheManager

This is where all the cache magic takes place. The API usually takes a URL and a set of ImageCacheManagerOptions.

ImageCacheManager.downloadAndCacheUrl(url: String, options: ImageCacheManagerOptions): Promise<String>

Check the cache for the the URL (after removing fixing the query string according to ImageCacheManagerOptions.useQueryParamsInCacheKey). If the URL exists in cache and is not expired, resolve with the local cached file path. Otherwise, download the file to the cache folder, add it to the cache and then return the cached file path.

ImageCacheManager.seedAndCacheUrl(url: String, seedPath: String, options: ImageCacheManagerOptions): Promise<String>

Check the cache for the the URL (after removing fixing the query string according to ImageCacheManagerOptions.useQueryParamsInCacheKey). If the URL exists in cache and is not expired, resolve with the local cached file path. Otherwise, copy the seed file into the cache folder, add it to the cache and then return the cached file path.

ImageCacheManager.deleteUrl(url: String, options: ImageCacheManagerOptions): Promise

Removes the cache entry for the URL and the local file corresponding to it, if it exists.

ImageCacheManager.clearCache(options: ImageCacheManagerOptions): Promise

Clear the URL cache and remove files in the cache folder (as stated in the ImageCacheManagerOptions.cacheLocation)

ImageCacheManager.getCacheInfo(options: ImageCacheManagerOptions): Promise.<{file: Array, size: Number}>

Returns info about the cache, list of files and the total size of the cache.

CachedImage

CachedImage is a drop in replacement for the Image component that will attempt to cache remote URLs for better performance.
It's main use is to hide the cache layer from the user and provide a simple way to cache images.
CachedImage uses an instance of ImageCacheManager to interact with the cache, if there is an instance provided by ImageCacheProvider via the context it will be used, otherwise a new instance will be created with the options from the component's props.

<CachedImage
    source={{
        uri: 'https://example.com/path/to/your/image.jpg'
    }}
    style={styles.image}
/>
Props
  • renderImage - a function that returns a component, used to override the underlying Image component.
  • activityIndicatorProps - props for the ActivityIndicator that is shown while the image is downloaded.
  • defaultSource - prop to display a background image while the source image is downloaded. This will work even in android, but will not display background image if there you set borderRadius on this component style prop
  • loadingIndicator - component prop to set custom ActivityIndicator.
  • fallbackSource - prop to set placeholder image. when source.uri is null or cached failed, the fallbackSource will be display.
  • any of the ImageCacheManagerOptionsPropTypes props - customize the ImageCacheManager for this specific CachedImage instance.

ImageCacheProvider

This is a top-level component with 2 major functions:

  1. Provide the customized ImageCacheManager to nested CachedImage.
  2. Preload a set of URLs.
Props
  • urlsToPreload - an array of URLs to preload when the component mounts. default []
  • numberOfConcurrentPreloads - control the number of concurrent downloads, usually used when the urlsToPreload array is very big. default urlsToPreload.length
  • onPreloadComplete - callback for when the preload is complete and all images are cached.

ImageCacheManagerOptions

A set of options that are provided to the ImageCacheManager and provide ways to customize it to your needs.

type ImageCacheManagerOptions = {
    headers: PropTypes.object,                      // an object to be used as the headers when sending the request for the url. default {}
    
    ttl: PropTypes.number,                          // the number of seconds each url will stay in the cache. default 2 weeks
    
    useQueryParamsInCacheKey: PropTypes.oneOfType([ // when handling a URL with query params, this indicates how it should be treated:
        PropTypes.bool,                             // if a bool value is given the whole query string will be used / ignored
        PropTypes.arrayOf(PropTypes.string)         // if an array of strings is given, only these keys will be taken from the query string.
    ]),                                             // default false
    
    cacheLocation: PropTypes.string,                // the path to the root of the cache folder. default the device cache dir 
    
    allowSelfSignedSSL: PropTypes.bool,             // true to allow self signed SSL URLs to be downloaded. default false
};

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].