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
If I use the source property to display image. Will the image be cached and save to disk after download?
If yes, what is the cache policy?
If I want to save the downloaded image to disk. Is it better to use getSize or prefetch method to do it?
Many thanks.
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.
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.
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.
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).
React Native does cache images after downloading and decoding them.
2.using Bitmap size as cache cost; by default, total 20MB cache capacity in React Native iOS
NSCache
in iOSsee more in How Image Loader and Cache work in React Native under the hood
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}
/>
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With