Screenshot is the graphics file that stored the current opening application on screen into a JPG or PNG format media image file, It will capture the screen and anything that display on the screen at the moment. Taking snapshots is a very important procedure for online ticket booking, online hotel booking, online movie ticket booking applications, where developer needs to store the booked ticket as a snapshot into mobile phone. So in this tutorial we would going to Take Screenshot of App Programmatically on button click in iOS Android react native application using react-native-view-shot NPM library. I am using this library because currently react native itself dose not give us any method to do this type of functionality, but thanks to react-native-view-shot library we can do this in both Android and iOS applications.
Contents in this project Take Screenshot of App Programmatically in iOS Android react native app on button click:
1. Open your react native project folder inside Command Prompt / Terminal and execute below command to install react-native-view-shot NPM library.
1
|
npm install react–native–view–shot
|
Screenshot of CMD :
2. Screenshot of CMD after successfully installing this library.
3. After successfully installed this library we need to execute below command in order to compile and index the current library into our project.
1
|
react–native link react–native–view–shot
|
Screenshot after executing above command:
4. Import StyleSheet, Text, View, Image, Button and Platform component in your project.
1
2
3
|
import React, { Component } from ‘react’;
import { StyleSheet, Text, View, Image, Button, Platform } from ‘react-native’;
|
5. Import captureScreen module from react-native-view-shot library.
1
|
import { captureScreen } from “react-native-view-shot”;
|
6. Create constructor() in your project class file, Now we would make a State named as imageURI. This state is used to Hold the image URI we will get after taking the image screenshot.
1
2
3
4
5
6
7
8
9
10
|
constructor(){
super();
this.state={
imageURI : ‘/wp-content/uploads/2018/02/motorcycle.jpg’
}
}
|
7. Create a function named as captureScreenFunction() and define the captureScreen() default inbuilt method of library inside this function. We would also update the imageURI state using stored screenshot URI.
1
2
3
4
5
6
7
8
9
10
11
12
|
captureScreenFunction=()=>{
captureScreen({
format: “jpg”,
quality: 0.8
})
.then(
uri => this.setState({ imageURI : uri }),
error => console.error(“Oops, Something Went Wrong”, error)
);
}
|
8. Creating a Root View in render’s return block. Now we would make a Button and Image component inside the View, We would set the Image URI as image source in image component. So when user takes the screenshot it will update the image source and display the stored image source in Image component.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
render() {
return (
<View style={styles.MainContainer}>
<Button title=“Capture Device Screenshot” onPress={this.captureScreenFunction} />
<Image source={{uri : this.state.imageURI}} style={{width: 200, height: 300, resizeMode: ‘contain’, marginTop: 5}} />
</View>
);
}
|
9. Creating Style.
1
2
3
4
5
6
7
8
9
10
11
|
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
backgroundColor: ‘#fff’,
borderWidth: 1,
borderColor: ‘#000’,
marginTop: (Platform.OS) === ‘ios’ ? 20 : 0,
}
});
|
10. Complete source code for App.js File :
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
|
import React, { Component } from ‘react’;
import { StyleSheet, Text, View, Image, Button, Platform } from ‘react-native’;
import { captureScreen } from “react-native-view-shot”;
export default class App extends Component {
constructor(){
super();
this.state={
imageURI : ‘/wp-content/uploads/2018/02/motorcycle.jpg’
}
}
captureScreenFunction=()=>{
captureScreen({
format: “jpg”,
quality: 0.8
})
.then(
uri => this.setState({ imageURI : uri }),
error => console.error(“Oops, Something Went Wrong”, error)
);
}
render() {
return (
<View style={styles.MainContainer}>
<Button title=“Capture Device Screenshot” onPress={this.captureScreenFunction} />
<Image source={{uri : this.state.imageURI}} style={{width: 200, height: 300, resizeMode: ‘contain’, marginTop: 5}} />
</View>
);
}
}
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
backgroundColor: ‘#fff’,
borderWidth: 1,
borderColor: ‘#000’,
marginTop: (Platform.OS) === ‘ios’ ? 20 : 0,
}
});
|
Screenshot in Android device:
Screenshot in iOS device: