forked from syanbo/react-native-syan-image-picker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (115 loc) · 3.84 KB
/
index.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import {
NativeModules,
Dimensions,
} from 'react-native';
const { RNSyanImagePicker } = NativeModules;
const { width } = Dimensions.get('window');
/**
* 默认参数
*/
const defaultOptions = {
imageCount: 6, // 最大选择图片数目,默认6
isRecordSelected: false, // 是否已选图片
isCamera: true, // 是否允许用户在内部拍照,默认true
isCrop: false, // 是否允许裁剪,默认false, imageCount 为1才生效
CropW: ~~(width * 0.6), // 裁剪宽度,默认屏幕宽度60%
CropH: ~~(width * 0.6), // 裁剪高度,默认屏幕宽度60%
isGif: false, // 是否允许选择GIF,默认false,暂无回调GIF数据
showCropCircle: false, // 是否显示圆形裁剪区域,默认false
circleCropRadius: width/2, // 圆形裁剪半径,默认屏幕宽度一半
showCropFrame: true, // 是否显示裁剪区域,默认true
showCropGrid: false, // 是否隐藏裁剪区域网格,默认false
quality: 90, // 压缩质量
enableBase64: false, // 是否返回base64编码,默认不返回
};
export default {
/**
* 以Callback形式调用
* 1、相册参数暂时只支持默认参数中罗列的属性;
* 2、回调形式:showImagePicker(options, (err, selectedPhotos) => {})
* 1)选择图片成功,err为null,selectedPhotos为选中的图片数组
* 2)取消时,err返回"取消",selectedPhotos将为undefined
* 按需判断各参数值,确保调用正常,示例使用方式:
* showImagePicker(options, (err, selectedPhotos) => {
* if (err) {
* // 取消选择
* return;
* }
* // 选择成功
* })
*
* @param {Object} options 相册参数
* @param {Function} callback 成功,或失败回调
*/
showImagePicker(options, callback) {
const optionObj = {
...defaultOptions,
...options
};
RNSyanImagePicker.showImagePicker(optionObj, callback)
},
/**
* 以Promise形式调用
* 1、相册参数暂时只支持默认参数中罗列的属性;
* 2、使用方式
* 1)async/await
* handleSelectPhoto = async () => {
* try {
* const photos = await SYImagePicker.asyncShowImagePicker(options);
* // 选择成功
* } catch (err) {
* // 取消选择,err.message为"取消"
* }
* }
* 2)promise.then形式
* handleSelectPhoto = () => {
* SYImagePicker.asyncShowImagePicker(options)
* .then(photos => {
* // 选择成功
* })
* .catch(err => {
* // 取消选择,err.message为"取消"
* })
* }
* @param {Object} options 相册参数
* @return {Promise} 返回一个Promise对象
*/
asyncShowImagePicker(options) {
const optionObj = {
...defaultOptions,
...options,
};
return RNSyanImagePicker.asyncShowImagePicker(optionObj);
},
/**
* 打开相机支持裁剪参数
* @param options
* @param callback
*/
openCamera(options, callback) {
const optionObj = {
...defaultOptions,
...options
};
RNSyanImagePicker.openCamera(optionObj, callback)
},
/**
* 清除缓存
*/
deleteCache() {
RNSyanImagePicker.deleteCache()
},
/**
* 移除选中的图片
* @param {Number} index 要移除的图片下标
*/
removePhotoAtIndex(index) {
RNSyanImagePicker.removePhotoAtIndex(index)
},
/**
* 移除所有选中图片
*/
removeAllPhoto() {
RNSyanImagePicker.removeAllPhoto()
}
};