2015-07-29 22:52:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The examples provided by Facebook are for non-commercial testing and
|
|
|
|
* evaluation purposes only.
|
|
|
|
*
|
|
|
|
* Facebook reserves all rights not expressly granted.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
|
|
|
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-04-09 03:36:40 +00:00
|
|
|
var React = require('react');
|
|
|
|
var ReactNative = require('react-native');
|
2015-07-29 22:52:28 +00:00
|
|
|
var {
|
|
|
|
CameraRoll,
|
|
|
|
Image,
|
2015-12-22 21:36:37 +00:00
|
|
|
ImageEditor,
|
2015-07-29 22:52:28 +00:00
|
|
|
NativeModules,
|
2016-01-28 17:42:28 +00:00
|
|
|
Platform,
|
2015-07-29 22:52:28 +00:00
|
|
|
ScrollView,
|
|
|
|
StyleSheet,
|
|
|
|
Text,
|
|
|
|
TouchableHighlight,
|
2015-11-27 13:39:00 +00:00
|
|
|
UIManager,
|
2015-07-29 22:52:28 +00:00
|
|
|
View,
|
2016-04-09 03:36:40 +00:00
|
|
|
} = ReactNative;
|
2015-12-22 21:36:37 +00:00
|
|
|
|
2015-07-29 22:52:28 +00:00
|
|
|
var PAGE_SIZE = 20;
|
|
|
|
|
|
|
|
type ImageOffset = {
|
|
|
|
x: number;
|
|
|
|
y: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
type ImageSize = {
|
|
|
|
width: number;
|
|
|
|
height: number;
|
|
|
|
};
|
|
|
|
|
2015-12-22 21:36:37 +00:00
|
|
|
type ImageCropData = {
|
2015-07-29 22:52:28 +00:00
|
|
|
offset: ImageOffset;
|
|
|
|
size: ImageSize;
|
2015-12-22 21:36:37 +00:00
|
|
|
displaySize?: ?ImageSize;
|
2016-02-25 10:44:58 +00:00
|
|
|
resizeMode?: ?any;
|
2015-12-22 21:36:37 +00:00
|
|
|
};
|
2015-07-29 22:52:28 +00:00
|
|
|
|
|
|
|
class SquareImageCropper extends React.Component {
|
2016-02-25 10:44:58 +00:00
|
|
|
state: any;
|
2015-07-29 22:52:28 +00:00
|
|
|
_isMounted: boolean;
|
2015-12-22 21:36:37 +00:00
|
|
|
_transformData: ImageCropData;
|
2015-07-29 22:52:28 +00:00
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this._isMounted = true;
|
|
|
|
this.state = {
|
|
|
|
randomPhoto: null,
|
|
|
|
measuredSize: null,
|
|
|
|
croppedImageURI: null,
|
|
|
|
cropError: null,
|
|
|
|
};
|
|
|
|
this._fetchRandomPhoto();
|
|
|
|
}
|
|
|
|
|
2016-01-27 18:49:31 +00:00
|
|
|
async _fetchRandomPhoto() {
|
|
|
|
try {
|
|
|
|
const data = await CameraRoll.getPhotos({first: PAGE_SIZE});
|
|
|
|
if (!this._isMounted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var edges = data.edges;
|
|
|
|
var edge = edges[Math.floor(Math.random() * edges.length)];
|
|
|
|
var randomPhoto = edge && edge.node && edge.node.image;
|
|
|
|
if (randomPhoto) {
|
|
|
|
this.setState({randomPhoto});
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.warn("Can't get a photo from camera roll", error);
|
|
|
|
}
|
2015-07-29 22:52:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this._isMounted = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (!this.state.measuredSize) {
|
|
|
|
return (
|
|
|
|
<View
|
|
|
|
style={styles.container}
|
|
|
|
onLayout={(event) => {
|
|
|
|
var measuredWidth = event.nativeEvent.layout.width;
|
|
|
|
if (!measuredWidth) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
measuredSize: {width: measuredWidth, height: measuredWidth},
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.state.croppedImageURI) {
|
|
|
|
return this._renderImageCropper();
|
|
|
|
}
|
|
|
|
return this._renderCroppedImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
_renderImageCropper() {
|
|
|
|
if (!this.state.randomPhoto) {
|
|
|
|
return (
|
|
|
|
<View style={styles.container} />
|
|
|
|
);
|
|
|
|
}
|
|
|
|
var error = null;
|
|
|
|
if (this.state.cropError) {
|
|
|
|
error = (
|
|
|
|
<Text>{this.state.cropError.message}</Text>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<View style={styles.container}>
|
|
|
|
<Text>Drag the image within the square to crop:</Text>
|
|
|
|
<ImageCropper
|
|
|
|
image={this.state.randomPhoto}
|
|
|
|
size={this.state.measuredSize}
|
|
|
|
style={[styles.imageCropper, this.state.measuredSize]}
|
|
|
|
onTransformDataChange={(data) => this._transformData = data}
|
|
|
|
/>
|
|
|
|
<TouchableHighlight
|
|
|
|
style={styles.cropButtonTouchable}
|
|
|
|
onPress={this._crop.bind(this)}>
|
|
|
|
<View style={styles.cropButton}>
|
|
|
|
<Text style={styles.cropButtonLabel}>
|
|
|
|
Crop
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
</TouchableHighlight>
|
|
|
|
{error}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_renderCroppedImage() {
|
|
|
|
return (
|
|
|
|
<View style={styles.container}>
|
|
|
|
<Text>Here is the cropped image:</Text>
|
|
|
|
<Image
|
|
|
|
source={{uri: this.state.croppedImageURI}}
|
|
|
|
style={[styles.imageCropper, this.state.measuredSize]}
|
|
|
|
/>
|
|
|
|
<TouchableHighlight
|
|
|
|
style={styles.cropButtonTouchable}
|
|
|
|
onPress={this._reset.bind(this)}>
|
|
|
|
<View style={styles.cropButton}>
|
|
|
|
<Text style={styles.cropButtonLabel}>
|
|
|
|
Try again
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
</TouchableHighlight>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_crop() {
|
2015-12-22 21:36:37 +00:00
|
|
|
ImageEditor.cropImage(
|
2015-07-29 22:52:28 +00:00
|
|
|
this.state.randomPhoto.uri,
|
|
|
|
this._transformData,
|
|
|
|
(croppedImageURI) => this.setState({croppedImageURI}),
|
|
|
|
(cropError) => this.setState({cropError})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_reset() {
|
|
|
|
this.setState({
|
|
|
|
randomPhoto: null,
|
|
|
|
croppedImageURI: null,
|
|
|
|
cropError: null,
|
|
|
|
});
|
|
|
|
this._fetchRandomPhoto();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class ImageCropper extends React.Component {
|
|
|
|
_contentOffset: ImageOffset;
|
2016-01-28 17:42:28 +00:00
|
|
|
_maximumZoomScale: number;
|
|
|
|
_minimumZoomScale: number;
|
|
|
|
_scaledImageSize: ImageSize;
|
|
|
|
_horizontal: boolean;
|
2015-07-29 22:52:28 +00:00
|
|
|
|
|
|
|
componentWillMount() {
|
|
|
|
// Scale an image to the minimum size that is large enough to completely
|
|
|
|
// fill the crop box.
|
|
|
|
var widthRatio = this.props.image.width / this.props.size.width;
|
|
|
|
var heightRatio = this.props.image.height / this.props.size.height;
|
2016-01-28 17:42:28 +00:00
|
|
|
this._horizontal = widthRatio > heightRatio;
|
|
|
|
if (this._horizontal) {
|
2015-07-29 22:52:28 +00:00
|
|
|
this._scaledImageSize = {
|
2016-01-28 17:42:28 +00:00
|
|
|
width: this.props.image.width / heightRatio,
|
|
|
|
height: this.props.size.height,
|
2015-07-29 22:52:28 +00:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
this._scaledImageSize = {
|
2016-01-28 17:42:28 +00:00
|
|
|
width: this.props.size.width,
|
|
|
|
height: this.props.image.height / widthRatio,
|
2015-07-29 22:52:28 +00:00
|
|
|
};
|
2016-01-28 17:42:28 +00:00
|
|
|
if (Platform.OS === 'android') {
|
|
|
|
// hack to work around Android ScrollView a) not supporting zoom, and
|
|
|
|
// b) not supporting vertical scrolling when nested inside another
|
|
|
|
// vertical ScrollView (which it is, when displayed inside UIExplorer)
|
|
|
|
this._scaledImageSize.width *= 2;
|
|
|
|
this._scaledImageSize.height *= 2;
|
|
|
|
this._horizontal = true;
|
|
|
|
}
|
2015-07-29 22:52:28 +00:00
|
|
|
}
|
|
|
|
this._contentOffset = {
|
|
|
|
x: (this._scaledImageSize.width - this.props.size.width) / 2,
|
|
|
|
y: (this._scaledImageSize.height - this.props.size.height) / 2,
|
|
|
|
};
|
2016-01-28 17:42:28 +00:00
|
|
|
this._maximumZoomScale = Math.min(
|
|
|
|
this.props.image.width / this._scaledImageSize.width,
|
|
|
|
this.props.image.height / this._scaledImageSize.height
|
|
|
|
);
|
|
|
|
this._minimumZoomScale = Math.max(
|
|
|
|
this.props.size.width / this._scaledImageSize.width,
|
2016-04-06 16:20:39 +00:00
|
|
|
this.props.size.height / this._scaledImageSize.height
|
2016-01-28 17:42:28 +00:00
|
|
|
);
|
2015-07-29 22:52:28 +00:00
|
|
|
this._updateTransformData(
|
|
|
|
this._contentOffset,
|
|
|
|
this._scaledImageSize,
|
|
|
|
this.props.size
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onScroll(event) {
|
|
|
|
this._updateTransformData(
|
|
|
|
event.nativeEvent.contentOffset,
|
|
|
|
event.nativeEvent.contentSize,
|
|
|
|
event.nativeEvent.layoutMeasurement
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateTransformData(offset, scaledImageSize, croppedImageSize) {
|
|
|
|
var offsetRatioX = offset.x / scaledImageSize.width;
|
|
|
|
var offsetRatioY = offset.y / scaledImageSize.height;
|
|
|
|
var sizeRatioX = croppedImageSize.width / scaledImageSize.width;
|
|
|
|
var sizeRatioY = croppedImageSize.height / scaledImageSize.height;
|
|
|
|
|
2015-12-22 21:36:37 +00:00
|
|
|
var cropData: ImageCropData = {
|
2015-07-29 22:52:28 +00:00
|
|
|
offset: {
|
|
|
|
x: this.props.image.width * offsetRatioX,
|
|
|
|
y: this.props.image.height * offsetRatioY,
|
|
|
|
},
|
|
|
|
size: {
|
|
|
|
width: this.props.image.width * sizeRatioX,
|
|
|
|
height: this.props.image.height * sizeRatioY,
|
|
|
|
},
|
2015-12-22 21:36:37 +00:00
|
|
|
};
|
|
|
|
this.props.onTransformDataChange && this.props.onTransformDataChange(cropData);
|
2015-07-29 22:52:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<ScrollView
|
|
|
|
alwaysBounceVertical={true}
|
|
|
|
automaticallyAdjustContentInsets={false}
|
|
|
|
contentOffset={this._contentOffset}
|
2016-01-28 13:35:14 +00:00
|
|
|
decelerationRate="fast"
|
2016-01-28 17:42:28 +00:00
|
|
|
horizontal={this._horizontal}
|
|
|
|
maximumZoomScale={this._maximumZoomScale}
|
|
|
|
minimumZoomScale={this._minimumZoomScale}
|
2015-07-29 22:52:28 +00:00
|
|
|
onMomentumScrollEnd={this._onScroll.bind(this)}
|
|
|
|
onScrollEndDrag={this._onScroll.bind(this)}
|
|
|
|
showsHorizontalScrollIndicator={false}
|
|
|
|
showsVerticalScrollIndicator={false}
|
|
|
|
style={this.props.style}
|
|
|
|
scrollEventThrottle={16}>
|
|
|
|
<Image source={this.props.image} style={this._scaledImageSize} />
|
|
|
|
</ScrollView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.framework = 'React';
|
2015-12-22 21:36:37 +00:00
|
|
|
exports.title = 'ImageEditor';
|
|
|
|
exports.description = 'Cropping and scaling with ImageEditor';
|
2015-07-29 22:52:28 +00:00
|
|
|
exports.examples = [{
|
|
|
|
title: 'Image Cropping',
|
|
|
|
render() {
|
|
|
|
return <SquareImageCropper/>;
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
alignSelf: 'stretch',
|
|
|
|
},
|
|
|
|
imageCropper: {
|
|
|
|
alignSelf: 'center',
|
|
|
|
marginTop: 12,
|
|
|
|
},
|
|
|
|
cropButtonTouchable: {
|
|
|
|
alignSelf: 'center',
|
|
|
|
marginTop: 12,
|
|
|
|
},
|
|
|
|
cropButton: {
|
|
|
|
padding: 12,
|
|
|
|
backgroundColor: 'blue',
|
|
|
|
borderRadius: 4,
|
|
|
|
},
|
|
|
|
cropButtonLabel: {
|
|
|
|
color: 'white',
|
|
|
|
fontSize: 16,
|
|
|
|
fontWeight: '500',
|
|
|
|
},
|
|
|
|
});
|