android: api 21+
ios : 10.0+
Image is a wrapper around Guiso (iOS) and Glide (Android).
- Memory cache
- Disk cache
- Resize
- uri
- Base64 String
- static Image
- Gif
- Webp image
$ npm install react-native-cached-imageview --save
$ react-native link react-native-cached-imageview
or
$ yarn add react-native-cached-imageview
Add Swift
/ios/name_project
add a .swift file
import { Controller, CachedImage ,DrawableImage } from 'react-native-cached-imageview'
//(Android) DrawableImage not support Gif - useful for shared elements.
const source = {
uri: 'https://unsplash.it/200/200?image=1',
width: 400,
height: 400,
}
const clearMemory = () =>{
Controller.clearMemoryCache()
}
<Image source={source} />
Source for the remote image to load.
uri to load the image from. e.g.
https://facebook.github.io/react/img/logo_og.png
.
static image ("static;${uri}")
base64String ("base64,${value}")
uri --> ("value")
if the image you load is an animated GIF, Image will display a animated gif. Default Value -> false
Headers to load the image with. e.g. { Authorization: 'someAuthToken' }.
low
normal -> Default
high
Priorities for completing loads. If more than one load is queued at a time, the load with the higher priority will end first. Priorities are considered best effort, there are no guarantees about the order in which loads will start or finish.
only accept static image ("static;${uri}") and base64String ("base64,${value}")
Default value -> null
Image that is shown while a request is in progress. When a request completes successfully, the placeholder is replaced with the requested resource. If the requested resource is loaded from memory, the placeholder may never be shown. If the request fails , the placeholder will continue to be displayed.
The width to be used in the resize, -1 ignore resize.
Default value -> -1
The height to be used in the resize, -1 ignore resize.
Default value -> -1
Determines how to resize the image:
cover: 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).
contain(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 less than the corresponding dimension of the view (minus padding).
Default value -> false
Allows the loaded resource to skip the memory cache.
Note - this is not a guarantee. If a request is already pending for this resource and that request is not also skipping the memory cache, the resource will be cached in memory.
Default value -> "automatic"
automatic
none
all
data
resource
Controls how the image should be displayed.
Default value -> "contain"
cover
contain
Called when the image starts to load.
Called on a successful image fetch. Called with the width and height of the loaded image.
e.g. onLoadSuccess={e => console.log(e.nativeEvent.width, e.nativeEvent.height)}
Called on an image fetching error.
e.g. onLoadError={e => console.log(e.nativeEvent.error)}
Called when the image finishes loading, whether it was successful or an error.
Promise
import { Controller } from 'react-native-cached-imageview'
Controller.clearMemoryCache()
Promise
import { Controller } from 'react-native-cached-imageview'
let compressFormat = "jpeg" | "png"
let quality = 0.9 // 0 to 1
Controller.requestImage(imageRef,compressFormat,quality).then( string64 => {
console.log("result ",string64)
})
Cancellation
import { Controller } from 'react-native-cached-imageview'
Controller.clearImage(imageRef)
callback
import { Controller } from 'react-native-cached-imageview'
Controller.get( {
uri: 'https://unsplash.it/200/200?image=1',
width: 400,
height: 400,
}, (result)=>{
if(result.error != null){
return
}
console.log("image string ",result.image)
})