Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native - Image Cache

Tags:

react-native

I read the document about the React Native image component in this site and got some questions: https://facebook.github.io/react-native/docs/image.html

  1. If I use the source property to display image. Will the image be cached and save to disk after download?

  2. If yes, what is the cache policy?

  3. If I want to save the downloaded image to disk. Is it better to use getSize or prefetch method to do it?

Many thanks.

like image 832
PrimaryChicken Avatar asked Dec 06 '16 05:12

PrimaryChicken


People also ask

Does React Native cache images?

react-native-fast-image is a performant React Native component for loading images. FastImage aggressively caches all loaded images. You can add your own request auth headers and preload images. react-native-fast-image even has GIF caching support.

How do I use cache image in React Native?

When writing apps with React Native you have access to the built-in Image component. React Native's Image component handles image caching like browsers for the most part. If the image is returned with a Cache-Control header than that will determine the caching behaviour.

How do I load images faster in React Native?

React Native FastImage is a quick way to load images in React Native. All loaded pictures are aggressively cached by FastImage. You may add your own auth headers and preload pictures to your requests. GIF caching is also supported by react-native-fast-image.

How do I preload an image in react?

To preload images with React and JavaScript, we can create our own hook. import { useEffect } from "react"; export const usePreloadImages = (imageSrcs) => { useEffect(() => { const randomStr = Math. random(). toString(32).


4 Answers

Process

React Native does cache images after downloading and decoding them. enter image description here

Cache policy

  1. expiration time

2.using Bitmap size as cache cost; by default, total 20MB cache capacity in React Native iOS

  1. unknown storage and eviction policy in NSCache in iOS

see more in How Image Loader and Cache work in React Native under the hood

like image 193
RY_ Zheng Avatar answered Oct 28 '22 01:10

RY_ Zheng


FastImage is best for Image Cache

yarn add react-native-fast-image

Ref: https://github.com/DylanVann/react-native-fast-image

Its maximum props behave like as React native image attribute

import FastImage from 'react-native-fast-image'


<FastImage
    style={{ width: 200, height: 200 }}
    source={{ uri: 'https://unsplash.it/400/400?image=1' }}
    resizeMode={FastImage.resizeMode.stretch}

/>
like image 42
Israt Jahan Simu Avatar answered Oct 28 '22 00:10

Israt Jahan Simu


React native image component NSURLRequest's cache policy as described here. Personally I use RNFetchBlob to cache images as described here. You can also checkout this component.

like image 42
wcandillon Avatar answered Oct 28 '22 01:10

wcandillon


You may be interested in my higher order component module that adds performance related image caching and "permanent cache" functionality to the native <Image> component.

React Native Image Cache HOC

Tl;DR Code Example:

import imageCacheHoc from 'react-native-image-cache-hoc';
const CacheableImage = imageCacheHoc(Image);

export default class App extends Component<{}> {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>Welcome to React Native!</Text>
        <CacheableImage style={styles.image} source={{uri: 'https://i.redd.it/rc29s4bz61uz.png'}} />
        <CacheableImage style={styles.image} source={{uri: 'https://i.redd.it/hhhim0kc5swz.jpg'}} permanent={true} />
      </View>
  );
  }
}

The first image will be cached until the total local cache grows past 15 MB (by default) then cached images are deleted oldest first until total cache is below 15 MB again.

The second image will be stored to local disk permanently. People use this as a drop in replacement for shipping static image files with your app.

That should handle your requirement out of the box. Hope it helps!

like image 33
billmalarky Avatar answered Oct 28 '22 00:10

billmalarky