-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathpickImage.ts
More file actions
42 lines (39 loc) · 1.25 KB
/
pickImage.ts
File metadata and controls
42 lines (39 loc) · 1.25 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
import { Platform } from 'react-native';
let ImagePicker;
try {
ImagePicker = require('react-native-image-picker');
} catch (e) {
console.log('react-native-image-picker is not installed');
}
export const pickImage = ImagePicker
? async () => {
try {
const result = await ImagePicker.launchImageLibrary({
assetRepresentationMode: 'current',
mediaType: 'mixed',
});
const canceled = result.didCancel;
const errorCode = result.errorCode;
if (Platform.OS === 'ios' && errorCode === 'permission') {
return { askToOpenSettings: true, cancelled: true };
}
if (!canceled) {
const assets = result.assets.map((asset) => ({
...asset,
duration: asset.duration ? asset.duration * 1000 : undefined, // in milliseconds
name: asset.fileName,
size: asset.fileSize,
source: 'picker',
type: asset.type,
uri: asset.uri,
}));
return { assets, cancelled: false, source: 'picker' };
} else {
return { cancelled: true };
}
} catch (error) {
console.log('Error picking image: ', error);
return { cancelled: true };
}
}
: null;