An image component for react-native with persistent disk and memory caching.
Java Objective-C TypeScript Ruby JavaScript
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
WebImage update SDWebImage to 4.3.3 Mar 20, 2018
android onError test Dec 28, 2017
dist rebuild js May 18, 2018
gradle/wrapper root project & gradle wrapper Dec 27, 2017
lib fix ts errors May 18, 2018
samples/SampleApp should work now Mar 20, 2018
tests/TestApp pod update May 18, 2018
.babelrc Upgrade to work with react-native 0.40.0, add eslint rules Jan 12, 2017
.editorconfig Add editorconfig to project Dec 7, 2017
.eslintrc.js Upgrade to work with react-native 0.40.0, add eslint rules Jan 12, 2017
.gitignore Ignore Android Studio captures folder Apr 10, 2017
.gitmodules Remove WebImage/Vendor/SDWebImage submodule (seems like npm don't ins… Sep 9, 2017
.npmignore Add jsconfig.json for navigation in vscode Mar 11, 2017
CHANGELOG.md Update CHANGELOG and README before release Oct 4, 2017
LICENSE Update copyright year. Jan 6, 2017
README.md Update README with api changes. Mar 21, 2018
TODO.md Fix bug when layout not updated. Track view size in onLayout instead … Apr 10, 2017
WebImage.podspec Add WebImage.podspec Dec 18, 2017
build.gradle fix react-native embedded maven repo url Dec 28, 2017
gradle.properties fix gradle.properties Dec 28, 2017
gradlew root project & gradle wrapper Dec 27, 2017
gradlew.bat root project & gradle wrapper Dec 27, 2017
jsconfig.json Add jsconfig.json for navigation in vscode Mar 11, 2017
package-lock.json bump npm deps May 18, 2018
package.json bump npm deps May 18, 2018
rn-cli.config.js hack with rn-wrapper no more needed, we can (and should actually) con… Dec 29, 2017
settings.gradle Restructure android project for simpler names. Dec 29, 2017
tsconfig.json fix ts errors May 18, 2018
tslint.json Rewrite in ts Mar 20, 2018

README.md

React Native WebImage

An image component for react-native with persistent disk and memory caching.

It is mostly a wrapper around native libraries which actually do the hard work. On Android it uses Glide, on iOS – SDWebImage.

⚠️ The package is currently in alpha stage of development. If you find a bug or missing functionality, please feel free to report, but better fix or implement what you want and send a pull request to GitHub repository.

Installation

npm install --save react-native-web-image
react-native link
version react-native
0.0.5 - 0.0.6 >=0.47.0
0.0.3 - 0.0.4 >=0.40.0
<=0.0.2 <0.40.0 (tested with 0.37.0)

Usage

import React, { Component } from 'react'
import { StyleSheet, Text, View } from 'react-native'
import WebImage from 'react-native-web-image'

export default class App extends Component {
  render() {
    const imageUri = 'https://placeholdit.imgix.net/~text?txtsize=33&txt=200x150&w=200&h=150'
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>Sample image</Text>
        <View style={styles.imgContainer}>
          <WebImage style={styles.img} source={{uri:imageUri}}/>
        </View>
      </View>
    )
  }
}

const white = '#FFFFFF'
const blue = 'rgb(0,0,255)'
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: white,
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  img: {
    flex: 1,
  },
  imgContainer: {
    flexDirection: 'row',
    flex: 1,
    borderWidth: 1,
    borderColor: blue,
  }
})

API

WebImage element

<WebImage source={source}/>
Attribute Type Description
source Object Describes image source (mimics original Image element)
source.uri String (Required) URL of the image
resizeMode Enum{'cover','contain','stretch','center'} Determine resize mode for image. Default: 'contain'
onError Function Will be called on error
onLoad Function Will be called when loading of image completed

Resize modes

  • 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).
  • cover - Scale the image uniformly 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).
  • stretch - Scale width and height independently, aspect ration will not be preserved.
  • center - Scale the image down so that it is completely visible, if bigger than the area of the view. The image will not be scaled up. The image will be placed at the center of the view.

onError(event)

  • event.nativeEvent.error - String representation of error (platform dependent)
  • event.nativeEvent.uri - URI which leads to error

onLoad(event)

  • event.nativeEvent.source.uri - URI of the loaded image
  • event.nativeEvent.source.width - width of the loaded image
  • event.nativeEvent.source.height - height of the loaded image

Author

Vladimir Timofeev

Contributors

License

  • Main source code is licensed under the MIT License.
  • SDWebImage (embedded in project) is licensed under the MIT License.
  • Glide (included via gradle) is licensed under Apache 2.0 License.