-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
LocalImagesExample.tsx
134 lines (123 loc) · 3.61 KB
/
LocalImagesExample.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
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
124
125
126
127
128
129
130
131
132
133
134
import React, { Component } from 'react'
import {
StyleSheet,
View,
Text,
TouchableOpacity,
ViewProps,
} from 'react-native'
import FastImage, { FastImageProps, Source } from 'react-native-fast-image'
import Section from './Section'
import FeatureText from './FeatureText'
import FieldsBase64 from './images/fields'
import { launchImageLibrary } from 'react-native-image-picker'
import BulletText from './BulletText'
// @ts-ignore
import FieldsImage from './images/fields.jpg'
// @ts-ignore
import FieldsWebP from './images/fields.webp'
// @ts-ignore
import JellyfishGIF from './images/jellyfish.gif'
// @ts-ignore
import JellyfishWebP from './images/jellyfish.webp'
const Image = ({ source, ...p }: FastImageProps) => (
<FastImage style={styles.imageSquare} source={source} {...p} />
)
const Row: React.ComponentType<ViewProps> = (p: ViewProps) => (
<View style={styles.row} {...p} />
)
interface ExampleProps {
name: string
source: any
}
const Example = ({ name, source }: ExampleProps) => (
<Row>
<BulletText>{name}</BulletText>
<Image source={source} />
</Row>
)
interface PhotoExampleState {
image?: Source
}
class PhotoExample extends Component<{}, PhotoExampleState> {
state: PhotoExampleState = {}
pick = () => {
launchImageLibrary({ mediaType: 'photo' }, (response) => {
if (response.didCancel) {
console.log('ImagePicker - User cancelled.')
} else if (response.errorCode) {
console.log(`ImagePicker - Error ${response.errorMessage}.`)
} else {
const uri = response?.assets?.[0]?.uri
if (uri) {
this.setState({
image: { uri: uri },
})
}
}
})
}
render() {
return (
<Row>
<BulletText>photo library</BulletText>
<TouchableOpacity onPress={this.pick}>
<Image
style={styles.imageSquare}
source={this.state.image || 0}
>
<Text style={styles.pickPhoto}>Pick Photo</Text>
</Image>
</TouchableOpacity>
</Row>
)
}
}
export const LocalImagesExample = () => (
<View>
<Section>
<FeatureText>• Local images.</FeatureText>
</Section>
<View style={styles.container}>
<Example name="Require" source={require('./images/fields.jpg')} />
<Example name="Import" source={FieldsImage} />
<Example name="GIF" source={JellyfishGIF} />
<Example name="Animated WebP" source={JellyfishWebP} />
<Example name="Base64" source={{ uri: FieldsBase64 }} />
<Example name="WebP" source={FieldsWebP} />
<PhotoExample />
</View>
</View>
)
const styles = StyleSheet.create({
pickPhoto: { color: 'white', fontWeight: '900' },
row: {
justifyContent: 'center',
alignItems: 'center',
marginBottom: 20,
},
container: {
backgroundColor: '#eee',
justifyContent: 'center',
alignItems: 'center',
paddingTop: 10,
paddingBottom: 10,
},
imageSquare: {
alignItems: 'center',
justifyContent: 'center',
height: 100,
backgroundColor: '#ddd',
margin: 20,
marginTop: 10,
width: 100,
flex: 0,
},
plus: {
width: 30,
height: 30,
position: 'absolute',
bottom: 0,
right: 0,
},
})