How to add Array to Native Image Picker

In this React Native source code example, the source code below illustrate how to add Array to Native Image Picker.

You can copy and adopt this source code example to your React Native project without reinventing the wheel.

import React, { Component } from 'react';
import { View, StyleSheet,Text,Image} from 'react-native'
import ImagePicker from 'react-native-image-picker';

const options = {
  storageOptions: {
    skipBackup: true,
    path: 'images'
  }
};

export default class App extends Component {
  state = {
    imageSource: []
}

  getImages(){
    ImagePicker.launchImageLibrary(options, (response) => {
      var source= response;
        this.setState({
          imageSource: this.state.imageSource.concat(source)
        });
      });
  }

  viewImages(){
   return this.state.imageSource.map(function(image){
      console.log(image);
        return(
          <View style={{width:'100%',padding: 10,backgroundColor: 'white'}}>
              <Image
                 source={{ uri: image.uri }}
                 style={{ width: 200, height: 200,margin: 10 }}
               />
          </View>
        )
      });

  }
   render() {
      return (
         <View style={styles.container}>
              <Text
                style={{padding: 10,backgroundColor: 'rgb(94, 200, 100)'}}
                onPress={()=>this.getImages()}>Find Images</Text>
                {this.viewImages()}
         </View>
      )
   }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: '#ddd'
  },
})

If you have any questions or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.