UNPKG

8.67 kBMarkdownView Raw
1<h1 align="center">
2 🚩 FastImage
3</h1>
4
5<div align="center">
6
7Performant React Native image component.
8
9[![Version][version-badge]][package]
10[![Downloads][downloads-badge]][npmtrends]
11[![Build Status][build-badge]][build]
12[![Code Coverage][coverage-badge]][coverage]
13
14[![Watch on GitHub][github-watch-badge]][github-watch]
15[![Star on GitHub][github-star-badge]][github-star]
16[![Tweet][twitter-badge]][twitter]
17
18</div>
19
20<p align="center" >
21 <kbd>
22 <img
23 src="https://github.com/DylanVann/react-native-fast-image/blob/main/docs/assets/scroll.gif?raw=true"
24 title="Scroll Demo"
25 float="left"
26 >
27 </kbd>
28 <kbd>
29 <img
30 src="https://github.com/DylanVann/react-native-fast-image/blob/main/docs/assets/priority.gif?raw=true"
31 title="Priority Demo"
32 float="left"
33 >
34 </kbd>
35 <br>
36 <em>FastImage example app.</em>
37</p>
38
39React Native's `Image` component handles image caching like browsers
40for the most part.
41If the server is returning proper cache control
42headers for images you'll generally get the sort of built in
43caching behavior you'd have in a browser.
44Even so many people have noticed:
45
46- Flickering.
47- Cache misses.
48- Low performance loading from cache.
49- Low performance in general.
50
51`FastImage` is an `Image` replacement that solves these issues.
52`FastImage` is a wrapper around
53[SDWebImage (iOS)](https://github.com/rs/SDWebImage)
54and
55[Glide (Android)](https://github.com/bumptech/glide).
56
57## Features
58
59- [x] Aggressively cache images.
60- [x] Add authorization headers.
61- [x] Prioritize images.
62- [x] Preload images.
63- [x] GIF support.
64- [x] Border radius.
65
66## Usage
67
68**Note: You must be using React Native 0.60.0 or higher to use the most recent version of `react-native-fast-image`.**
69
70```bash
71yarn add react-native-fast-image
72cd ios && pod install
73```
74
75```jsx
76import FastImage from 'react-native-fast-image'
77
78const YourImage = () => (
79 <FastImage
80 style={{ width: 200, height: 200 }}
81 source={{
82 uri: 'https://unsplash.it/400/400?image=1',
83 headers: { Authorization: 'someAuthToken' },
84 priority: FastImage.priority.normal,
85 }}
86 resizeMode={FastImage.resizeMode.contain}
87 />
88)
89```
90
91## Are you using Glide already using an AppGlideModule?
92
93- [Are you using Glide already using an AppGlideModule?](docs/app-glide-module.md) (you might have problems if you don't read this)
94
95## Are you using Proguard?
96
97If you use Proguard you will need to add these lines to `android/app/proguard-rules.pro`:
98
99```
100-keep public class com.dylanvann.fastimage.* {*;}
101-keep public class com.dylanvann.fastimage.** {*;}
102-keep public class * implements com.bumptech.glide.module.GlideModule
103-keep public class * extends com.bumptech.glide.module.AppGlideModule
104-keep public enum com.bumptech.glide.load.ImageHeaderParser$** {
105 **[] $VALUES;
106 public *;
107}
108```
109
110## Properties
111
112### `source?: object`
113
114Source for the remote image to load.
115
116---
117
118### `source.uri?: string`
119
120Remote url to load the image from. e.g. `'https://facebook.github.io/react/img/logo_og.png'`.
121
122---
123
124### `source.headers?: object`
125
126Headers to load the image with. e.g. `{ Authorization: 'someAuthToken' }`.
127
128---
129
130### `source.priority?: enum`
131
132- `FastImage.priority.low` - Low Priority.
133- `FastImage.priority.normal` **(Default)** - Normal Priority.
134- `FastImage.priority.high` - High Priority.
135
136---
137
138### `source.cache?: enum`
139
140- `FastImage.cacheControl.immutable` - **(Default)** - Only updates if url changes.
141- `FastImage.cacheControl.web` - Use headers and follow normal caching procedures.
142- `FastImage.cacheControl.cacheOnly` - Only show images from cache, do not make any network requests.
143
144---
145
146### `defaultSource?: number`
147
148- An asset loaded with `require(...)`.
149- Note that like the built-in `Image` implementation, on Android `defaultSource` does not work in debug mode. This is due to the fact that assets are sent from the dev server, but RN's functions only know how to load it from `res`.
150
151---
152
153### `resizeMode?: enum`
154
155- `FastImage.resizeMode.contain` - Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding).
156- `FastImage.resizeMode.cover` **(Default)** - Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding).
157- `FastImage.resizeMode.stretch` - Scale width and height independently, This may change the aspect ratio of the src.
158- `FastImage.resizeMode.center` - Do not scale the image, keep centered.
159
160---
161
162### `onLoadStart?: () => void`
163
164Called when the image starts to load.
165
166---
167
168### `onProgress?: (event) => void`
169
170Called when the image is loading.
171
172e.g. `onProgress={e => console.log(e.nativeEvent.loaded / e.nativeEvent.total)}`
173
174---
175
176### `onLoad?: (event) => void`
177
178Called on a successful image fetch. Called with the width and height of the loaded image.
179
180e.g. `onLoad={e => console.log(e.nativeEvent.width, e.nativeEvent.height)}`
181
182---
183
184### `onError?: () => void`
185
186Called on an image fetching error.
187
188---
189
190### `onLoadEnd?: () => void`
191
192Called when the image finishes loading, whether it was successful or an error.
193
194---
195
196### `style`
197
198A React Native style. Supports using `borderRadius`.
199
200---
201
202### `fallback: boolean`
203
204If true will fallback to using `Image`.
205In this case the image will still be styled and laid out the same way as `FastImage`.
206
207---
208
209### `tintColor?: number | string`
210
211If supplied, changes the color of all the non-transparent pixels to the given color.
212
213## Static Methods
214
215### `FastImage.preload: (source[]) => void`
216
217Preload images to display later. e.g.
218
219```js
220FastImage.preload([
221 {
222 uri: 'https://facebook.github.io/react/img/logo_og.png',
223 headers: { Authorization: 'someAuthToken' },
224 },
225 {
226 uri: 'https://facebook.github.io/react/img/logo_og.png',
227 headers: { Authorization: 'someAuthToken' },
228 },
229])
230```
231
232### `FastImage.clearMemoryCache: () => Promise<void>`
233
234Clear all images from memory cache.
235
236### `FastImage.clearDiskCache: () => Promise<void>`
237
238Clear all images from disk cache.
239
240## Troubleshooting
241
242If you have any problems using this library try the steps in [troubleshooting](docs/troubleshooting.md) and see if they fix it.
243
244## Development
245
246[Follow these instructions to get the example app running.](docs/development.md)
247
248## Supported React Native Versions
249
250This project only aims to support the latest version of React Native.\
251This simplifies the development and the testing of the project.
252
253If you require new features or bug fixes for older versions you can fork this project.
254
255## Credits
256
257The idea for this modules came from
258[vovkasm's](https://github.com/vovkasm)
259[react-native-web-image](https://github.com/vovkasm/react-native-web-image)
260package.
261It also uses Glide and SDWebImage, but didn't have some features I needed (priority, headers).
262
263Thanks to [@mobinni](https://github.com/mobinni) for helping with the conceptualization
264
265## Licenses
266
267- FastImage - MIT © [DylanVann](https://github.com/DylanVann)
268- SDWebImage - `MIT`
269- Glide - BSD, part MIT and Apache 2.0. See the [LICENSE](https://github.com/bumptech/glide/blob/master/license) file for details.
270
271[build-badge]: https://github.com/dylanvann/react-native-fast-image/workflows/CI/badge.svg
272[build]: https://github.com/DylanVann/react-native-fast-image/actions?query=workflow%3ACI
273[coverage-badge]: https://img.shields.io/codecov/c/github/dylanvann/react-native-fast-image.svg
274[coverage]: https://codecov.io/github/dylanvann/react-native-fast-image
275[downloads-badge]: https://img.shields.io/npm/dm/react-native-fast-image.svg
276[npmtrends]: http://www.npmtrends.com/react-native-fast-image
277[package]: https://www.npmjs.com/package/react-native-fast-image
278[version-badge]: https://img.shields.io/npm/v/react-native-fast-image.svg
279[twitter]: https://twitter.com/home?status=Check%20out%20react-native-fast-image%20by%20%40atomarranger%20https%3A//github.com/DylanVann/react-native-fast-image
280[twitter-badge]: https://img.shields.io/twitter/url/https/github.com/DylanVann/react-native-fast-image.svg?style=social
281[github-watch-badge]: https://img.shields.io/github/watchers/dylanvann/react-native-fast-image.svg?style=social
282[github-watch]: https://github.com/dylanvann/react-native-fast-image/watchers
283[github-star-badge]: https://img.shields.io/github/stars/dylanvann/react-native-fast-image.svg?style=social
284[github-star]: https://github.com/dylanvann/react-native-fast-image/stargazers