-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.js
192 lines (170 loc) · 5.24 KB
/
App.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import React, { useRef, useEffect, useState } from "react";
import {
StyleSheet,
Image,
Text,
View,
Platform,
TouchableOpacity,
} from "react-native";
import PinturaEditor from "@pqina/react-native-expo-pintura";
import { useAssets } from "expo-asset";
import * as ImagePicker from "expo-image-picker";
import * as FileSystem from "expo-file-system";
import {
createMarkupEditorToolStyle,
createMarkupEditorToolStyles,
} from "@pqina/pintura";
const buttonStyle = {
backgroundColor: "#222",
paddingTop: 5,
paddingBottom: 5,
paddingHorizontal: 15,
borderRadius: 10,
marginHorizontal: 5,
};
const buttonTextStyle = {
fontSize: 14,
color: "#fff",
};
export default function App() {
const [assets, error] = useAssets([require("./assets/image.jpeg")]);
const [editorEnabled, setEditorEnabled] = useState(true);
const [editorSource, setEditorSource] = useState(undefined);
const [editorResult, setEditorResult] = useState(undefined);
const editorRef = useRef(null);
// This requests permission to select camera roll images
useEffect(() => {
(async () => {
// Not needed on Web
if (Platform.OS === "web") return;
// All fine when access granted
const { status } =
await ImagePicker.requestMediaLibraryPermissionsAsync();
if (status === "granted") return;
// Show message if not allowed
alert("Sorry, we need camera roll permissions to make this work!");
})();
}, []);
return (
<View style={styles.container}>
{editorResult && (
<Image
style={{ width: 100, height: 100 }}
source={{ uri: editorResult }}
/>
)}
{/* The Pintura Editor component */}
{editorEnabled && (
<PinturaEditor
ref={editorRef}
style={{
width: "95%",
height: "80%",
borderWidth: 1,
borderColor: "#eee",
}}
styleRules={`
.pintura-editor {
--color-background: 255, 255, 255;
--color-foreground: 0, 0, 0;
}
`}
markupEditorToolStyles={createMarkupEditorToolStyles({
text: createMarkupEditorToolStyle("text", {
fontSize: "10%",
}),
})}
imageCropAspectRatio={1}
src={editorSource}
onLoaderror={(err) => {
console.log("onLoaderror", err);
}}
onLoad={({ size }) => {
console.log("onLoad", size);
}}
onProcess={({ dest, imageState }) => {
// dest is output file in dataURI format
console.log("onProcess", imageState, "size", dest.length);
// preview
setEditorResult(dest);
}}
/>
)}
<View style={{ flexDirection: "row", marginTop: 20 }}>
{/* Example removing and adding the editor */}
<TouchableOpacity
style={buttonStyle}
onPress={() => {
setEditorEnabled(!editorEnabled);
}}
>
<Text style={buttonTextStyle}>Toggle</Text>
</TouchableOpacity>
{/* Example updating editor image source */}
<TouchableOpacity
style={buttonStyle}
onPress={() => {
// load local asset
const [image] = assets;
const { localUri } = image;
FileSystem.readAsStringAsync(localUri, {
encoding: FileSystem.EncodingType.Base64,
}).then((base64) => {
setEditorSource(`data:image/jpeg;base64,${base64}`);
});
}}
>
<Text style={buttonTextStyle}>Test image</Text>
</TouchableOpacity>
{/* Example running an editor function */}
<TouchableOpacity
style={buttonStyle}
onPress={() => {
// Run editor function
editorRef.current.editor.history.undo();
}}
>
<Text style={buttonTextStyle}>Undo</Text>
</TouchableOpacity>
{/* Example selecting a library image */}
<TouchableOpacity
style={buttonStyle}
onPress={async () => {
// Use ImagePicker to get a base64 image string
const res = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.All,
allowsMultipleSelection: false,
quality: 1,
base64: true,
});
const { canceled, assets } = res;
if (canceled) return;
const [asset] = assets;
// video to base64
let base64 = asset.base64;
let type = "image/jpeg";
if (!base64) {
base64 = await FileSystem.readAsStringAsync(asset.uri, {
encoding: FileSystem.EncodingType.Base64,
});
type = "video/mp4";
}
// send data url to editor
setEditorSource(`data:${type};base64,${base64}`);
}}
>
<Text style={buttonTextStyle}>Browse...</Text>
</TouchableOpacity>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
});