forked from DmitriyGalyanov/react-native-image-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
58 lines (52 loc) · 2.09 KB
/
index.ts
File metadata and controls
58 lines (52 loc) · 2.09 KB
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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import { NativeModules, Platform, Image } from 'react-native';
const { RNCImageEditor } = NativeModules;
export type ImageCropData = {
/**
* The top-left corner of the cropped image, specified in the original
* image's coordinate space.
*/
offset: { x: number, y: number },
/**
* The size (dimensions) of the cropped image, specified in the original
* image's coordinate space.
*/
size: { width: number, height: number },
/**
* Size to scale the cropped image to.
* only works on iOS (and will be removed afterwards),
* defaults to 1280 for larger size on Android
*/
displaySize: { width: number, height: number },
};
/* RN.Image.getSize iOS version doesn't seem to return incorrect dimensions thus doesn't require replacement */
const iosGetSize = (uri: string) =>
new Promise((resolve) =>
Image.getSize(uri, (width: number, height: number) => resolve({ width, height }))
)
export default class ImageEditor {
static getImageDimensions(uri: string): Promise<{ width: number; height: number; }> {
return Platform.OS === 'android' ? RNCImageEditor.getImageDimensions(uri) : iosGetSize(uri);
}
/**
* Crop the image specified by the URI param. If URI points to a remote
* image, it will be downloaded automatically. If the image cannot be
* loaded/downloaded, the promise will be rejected. On Android, a
* downloaded image may be cached in external storage, a publicly accessible
* location, if it has more available space than internal storage.
*
* If the cropping process is successful, the resultant cropped image
* will be stored in the Cache Path, and the URI returned in the promise
* will point to the image in the cache path. Remember to delete the
* cropped image from the cache path when you are done with it.
*/
static cropImage(uri: string, cropData: ImageCropData): Promise<string> {
return RNCImageEditor.cropImage(uri, cropData);
}
}