-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathcrop-view.component.tsx
68 lines (58 loc) · 1.63 KB
/
crop-view.component.tsx
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
import React, { createRef } from 'react';
import {
findNodeHandle,
NativeSyntheticEvent,
requireNativeComponent,
StyleProp,
UIManager,
ViewStyle,
} from 'react-native';
const RCTCropView = requireNativeComponent('CropView');
type Response = {
uri: string;
width: number;
height: number;
};
type Props = {
sourceUrl: string;
style?: StyleProp<ViewStyle>;
onImageCrop?: (res: Response) => void;
keepAspectRatio?: boolean;
aspectRatio?: { width: number; height: number };
};
class CropView extends React.PureComponent<Props> {
public static defaultProps = {
keepAspectRatio: false,
};
private viewRef = createRef<any>();
public saveImage = (preserveTransparency: boolean = true, quality: number = 90) => {
UIManager.dispatchViewManagerCommand(
findNodeHandle(this.viewRef.current!),
UIManager.getViewManagerConfig('CropView').Commands.saveImage,
[preserveTransparency, quality]
);
};
public rotateImage = (clockwise: boolean = true) => {
UIManager.dispatchViewManagerCommand(
findNodeHandle(this.viewRef.current!),
UIManager.getViewManagerConfig('CropView').Commands.rotateImage,
[clockwise]
);
};
public render() {
const { sourceUrl, style, onImageCrop, keepAspectRatio, aspectRatio } = this.props;
return (
<RCTCropView
ref={this.viewRef}
sourceUrl={sourceUrl}
style={style}
onImageSaved={(event: NativeSyntheticEvent<Response>) => {
onImageCrop!(event.nativeEvent);
}}
keepAspectRatio={keepAspectRatio}
cropAspectRatio={aspectRatio}
/>
);
}
}
export default CropView;