Skip to content

Instantly share code, notes, and snippets.

@alien3d
Created October 6, 2017 03:43
Show Gist options
  • Save alien3d/b27642ec02e50f74cecd16c1d79ac21f to your computer and use it in GitHub Desktop.
Save alien3d/b27642ec02e50f74cecd16c1d79ac21f to your computer and use it in GitHub Desktop.
image crop
import React from 'react';
import { ImageEditor, Button, Image, View } from 'react-native';
import { ImagePicker } from 'expo';
export default class ImagePickerExample extends React.Component {
state = {
image: null,
};
render() {
let { image } = this.state;
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button
title="Pick an image from camera roll"
onPress={this._pickImage}
/>
{image &&
<Image source={{ uri: image }} style={{ width: 200, height: 200, resizeMode: 'contain' }} />}
</View>
);
}
_pickImage = async () => {
let result = await ImagePicker.launchImageLibraryAsync({
allowsEditing: true,
aspect: [4, 3],
});
if (result.cancelled) {
console.log('got here');
return;
}
let resizedUri = await new Promise((resolve, reject) => {
ImageEditor.cropImage(result.uri,
{
offset: { x: 0, y: 0 },
size: { width: result.width, height: result.height },
displaySize: { width: 50, height: 50 },
resizeMode: 'contain',
},
(uri) => resolve(uri),
() => reject(),
);
});
// this gives you a rct-image-store URI or a base64 image tag that
// you can use from ImageStore
this.setState({ image: resizedUri });
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment