Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cache image on react native

Is there a good library or maybe some default react native components that cache the image from a url? I've tried react-native-cache-image but there are a lot of issues with react-native-fs and react-native-sqlite-storage and as I am new to react native I dont know how to fix them properly.

like image 906
Adrian Zghibarta Avatar asked Oct 26 '16 08:10

Adrian Zghibarta


People also ask

How do I cache an image in React Native?

Using react-native-cached-image When both packages are successfully installed, you can import CachedImage and replace any instances of Image or ImageBackground that you want cached. import { CachedImage } from 'react-native-cached-image'; In my example app, I set up a FlatList to show the images.

How do I quickly load images 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.

What is caching in React Native?

Image caching essentially means downloading an image to the local storage in the app's cache directory (or any other directory that is accessible to the app) and loading it from local storage next time the image loads. There are a few ways to approach image caching in React Native.

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).


2 Answers

yarn add react-native-fast-image

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

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 69
Israt Jahan Simu Avatar answered Sep 22 '22 20:09

Israt Jahan Simu


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. My module depends on react-native-fetch-blob which is the goto well-respected and battle-tested library for downloading files, so you shouldn't have dependency problems.

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.

It also sounds like you are interested in arbitrarily storing image files to local disk. You can do that with a CacheableImage static method like so:

import imageCacheHoc from 'react-native-image-cache-hoc';
const CacheableImage = imageCacheHoc(Image);
CacheableImage.cacheFile('https://i.redd.it/hhhim0kc5swz.jpg', true)
  .then( localFileInfo => {
    console.log(localFileInfo);

    // Console log outputs:
    //{
    //  url: 'https://i.redd.it/rc29s4bz61uz.png',
    //  cacheType: 'permanent',
    //  localFilePath: '/this/is/absolute/path/to/file.jpg'
    //}

  });

Hope this helps!

like image 32
billmalarky Avatar answered Sep 22 '22 20:09

billmalarky