Skip to content

Commit 5ed8cf9

Browse files
committed
Update the example App to include the Native Screenshot button for testing the captureScreen functionality. Update the index.js as a final code refactor for the captureScreen name,
1 parent 15a39d8 commit 5ed8cf9

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

example/App.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
WebView
1313
} from "react-native";
1414
import omit from "lodash/omit";
15-
import { captureRef } from "react-native-view-shot";
15+
import { captureRef, captureScreen } from "react-native-view-shot";
1616
import { Surface } from "gl-react-native";
1717
import GL from "gl-react";
1818
import MapView from "react-native-maps";
@@ -53,6 +53,43 @@ export default class App extends Component {
5353
}
5454
};
5555

56+
captureScreenshot = () =>
57+
captureScreen(this.state.value)
58+
.then(
59+
res =>
60+
this.state.value.result !== "tmpfile"
61+
? res
62+
: new Promise((success, failure) =>
63+
// just a test to ensure res can be used in Image.getSize
64+
Image.getSize(
65+
res,
66+
(width, height) => (
67+
console.log(res, width, height), success(res)
68+
),
69+
failure
70+
)
71+
)
72+
)
73+
.then(res =>
74+
this.setState({
75+
error: null,
76+
res,
77+
previewSource: {
78+
uri:
79+
this.state.value.result === "base64"
80+
? "data:image/" + this.state.value.format + ";base64," + res
81+
: res
82+
}
83+
})
84+
)
85+
.catch(
86+
error => (
87+
console.warn(error),
88+
this.setState({ error, res: null, previewSource: null })
89+
)
90+
);
91+
92+
5693
snapshot = refname => () =>
5794
captureRef(this.refs[refname], this.state.value)
5895
.then(
@@ -147,6 +184,7 @@ export default class App extends Component {
147184
<Btn label="📷 MapView" onPress={this.snapshot("mapview")} />
148185
<Btn label="📷 WebView" onPress={this.snapshot("webview")} />
149186
<Btn label="📷 Video" onPress={this.snapshot("video")} />
187+
<Btn label="📷 Native Screenshot" onPress={this.captureScreenshot}/>
150188
<Btn
151189
label="📷 Empty View (should crash)"
152190
onPress={this.snapshot("empty")}

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export function captureScreen(
124124
errors.map(e => `- ${e}`).join("\n")
125125
);
126126
}
127-
return RNViewShot.captureScreenshot(options);
127+
return RNViewShot.captureScreen(options);
128128
}
129129

130130
type Props = {

0 commit comments

Comments
 (0)