Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 37 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ export default class PhotoUpload extends React.Component {
ImagePicker.showImagePicker(this.options, async response => {
this.setState({buttonDisabled: false})

let rotation = 0
let rotation = 0
const {originalRotation} = response


if (this.props.onResponse) this.props.onResponse(response)

Expand All @@ -76,42 +76,45 @@ export default class PhotoUpload extends React.Component {
}

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
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
rotation = -90
}
try { // 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)
} catch (e) {
if (this.props.onError) this.props.onError(e)
}
// 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)
})
}

Expand Down