Skip to content

Instantly share code, notes, and snippets.

@santiblanko
Created September 29, 2017 03:22
Show Gist options
  • Save santiblanko/723c53eced34104137f1ab2b1dd9be44 to your computer and use it in GitHub Desktop.
Save santiblanko/723c53eced34104137f1ab2b1dd9be44 to your computer and use it in GitHub Desktop.
import React from 'react'
import PropTypes from 'prop-types'
import {
View,
Image,
StyleSheet,
TouchableOpacity,
Platform
} from 'react-native'
import ImagePicker from 'react-native-image-picker'
import ImageResizer from 'react-native-image-resizer'
import RNFS from 'react-native-fs'
export default class PhotoUpload extends React.Component {
static propTypes = {
containerStyle: PropTypes.object,
photoPickerTitle: PropTypes.string,
height: PropTypes.number,
width: PropTypes.number,
format: PropTypes.string,
quality: PropTypes.number,
onPhotoSelect: PropTypes.func // returns the base64 string of uploaded photo
}
state = {
height: this.props.height || 300,
width: this.props.width || 300,
format: this.props.format || 'PNG',
quality: this.props.quality || 80
}
options = {
title: this.props.pickerTitle || 'Seleccione foto',
cancelButtonTitle: 'Cancelar',
takePhotoButtonTitle: 'Capturar foto',
chooseFromLibraryButtonTitle: 'Escoger foto'
}
openImagePicker = () => {
// get image from image picker
ImagePicker.showImagePicker(this.options, async response => {
console.log('Response = ', response)
if (response.didCancel) {
console.log('User cancelled image picker')
return
} else if (response.error) {
console.log('ImagePicker Error: ', response.error)
return
} else if (response.customButton) {
console.log('User tapped custom button: ', response.customButton)
return
}
if (this.props.onPhotoSelect) {
this.props.onPhotoSelect(response)
}
})
}
renderChildren = props => {
return React.Children.map(props.children, child => {
if (child.type === Image && this.state.avatarSource) {
return React.cloneElement(child, {
source: this.state.avatarSource
})
} else return child
})
}
render() {
return (
<View style={[styles.container, this.props.containerStyle]}>
<TouchableOpacity onPress={this.openImagePicker}>
{this.renderChildren(this.props)}
</TouchableOpacity>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment