-
Notifications
You must be signed in to change notification settings - Fork 88
/
index.js
152 lines (133 loc) · 4.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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,
maxHeight: PropTypes.number,
maxWidth: PropTypes.number,
format: PropTypes.string,
quality: PropTypes.number,
onPhotoSelect: PropTypes.func, // returns the base64 string of uploaded photo
onError: PropTypes.func, // if any error occur with response
onTapCustomButton: PropTypes.func, // on tap custom button
onStart: PropTypes.func, // when user starts (useful for loading, etc)
onCancel: PropTypes.func, // when user cancel
onResponse: PropTypes.func, // on response exists!
onRender: PropTypes.func, // after render
onResizedImageUri: PropTypes.func, // when image resized is ready
imagePickerProps: PropTypes.object // react-native-image-picker props
}
state = {
maxHeight: this.props.height || 600,
maxWidth: this.props.width || 600,
format: this.props.format || 'JPEG',
quality: this.props.quality || 100,
buttonDisabled: false
}
options = {
title: this.props.photoPickerTitle || 'Select Photo',
storageOptions: {
skipBackup: true,
path: 'images'
},
...this.props.imagePickerProps
}
openImagePicker = () => {
this.setState({buttonDisabled: true})
if (this.props.onStart) this.props.onStart()
// get image from image picker
ImagePicker.showImagePicker(this.options, async response => {
this.setState({buttonDisabled: false})
let rotation = 0
const {originalRotation} = response
if (this.props.onResponse) this.props.onResponse(response)
if (response.didCancel) {
console.log('User cancelled image picker')
if (this.props.onCancel) this.props.onCancel('User cancelled image picker')
return
} else if (response.error) {
console.log('ImagePicker Error: ', response.error)
if (this.props.onError) this.props.onError(response.error)
return
} else if (response.customButton) {
console.log('User tapped custom button: ', response.customButton)
if (this.props.onTapCustomButton) this.props.onTapCustomButton(response.customButton)
return
}
let { maxHeight, maxWidth, quality, format } = this.state
//Determining rotation param
if ( originalRotation === 90) {
rotation = 90
} else if (originalRotation === 180) {
//For a few images rotation is 180.
rotation = -180
} else if ( originalRotation === 270 ) {
//When taking images with the front camera (selfie), the rotation is 270.
rotation = -90
}
// resize image
const resizedImageUri = await ImageResizer.createResizedImage(
`data:image/jpeg;base64,${response.data}`,
maxHeight,
maxWidth,
format,
quality,
rotation
)
if (this.props.onResizedImageUri) this.props.onResizedImageUri(resizedImageUri)
const filePath = Platform.OS === 'android' && resizedImageUri.uri.replace
? resizedImageUri.uri.replace('file:/data', '/data')
: resizedImageUri.uri
// convert image back to base64 string
const photoData = await RNFS.readFile(filePath, 'base64')
let source = { uri: resizedImageUri.uri }
this.setState({
avatarSource: source
})
// handle photo in props functions as data string
if (this.props.onPhotoSelect) this.props.onPhotoSelect(photoData)
})
}
renderChildren = props => {
return React.Children.map(props.children, child => {
if (child && child.type === Image && this.state.avatarSource) {
return React.cloneElement(child, {
source: this.state.avatarSource
})
} else return child
})
}
componentDidUpdate() {
if (this.props.onAfterRender) this.props.onAfterRender(this.state)
}
render() {
return (
<View style={[styles.container, this.props.containerStyle]}>
<TouchableOpacity
onPress={this.openImagePicker}
disabled={this.state.buttonDisabled}
>
{this.renderChildren(this.props)}
</TouchableOpacity>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}
})