React Native View Shot
Here is an Example to Take Screenshot Programmatically in React Native. To take Screenshot of your screen we have an inbuilt Screenshot option provided by mobile manufacturers but you can also make this in your own Application too. To take a Screenshot we will use a very good library called react-native-view-shot
. Which provides captureScreen API which is very easy to use.
To Make a React Native App
Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react native command line interface to make our React Native App.
If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:
npm uninstall -g react-native-cli @react-native-community/cli
Run the following commands to create a new React Native project
npx react-native init ProjectName
If you want to start a new project with a specific React Native version, you can use the --version argument:
npx react-native init ProjectName --version X.XX.X
Note If the above command is failing, you may have old version of react-native
or react-native-cli
installed globally on your pc. Try uninstalling the cli and run the cli using npx.
This will make a project structure with an index file named App.js in your project directory.
Installation of Dependency
To use captureScreen
we need to install react-native-view-shot
package.
To install this open the terminal and jump into your project
cd Projectname
Run the following command
npm install react-native-view-shot --save
This command will copy all the dependencies into your node_module directory, You can find the directory in node_module
the directory named react-native-view-shot
.
–save is optional, it is just to update the react-native-view-shot dependency in your package.json file.
CocoaPods Installation
Please use the following command to install CocoaPods
npx pod-install
Code to Take Screenshot Programmatically
Open App.js in any code editor and replace the code with the following code
App.js
// React Native Example to Take Screenshot Programmatically
// https://aboutreact.com/take-screenshot-programmatically/
// import React in our code
import React, {useState} from 'react';
// import all the components we are going to use
import {
SafeAreaView,
StyleSheet,
Text,
View,
Image,
TouchableOpacity,
} from 'react-native';
// import CaptureScreen
import {captureScreen} from 'react-native-view-shot';
const App = () => {
const [imageURI, setImageURI] = useState(
'https://raw.githubusercontent.com/AboutReact/sampleresource/master/sample_img.png',
);
const [savedImagePath, setSavedImagePath] = useState('');
const takeScreenShot = () => {
// To capture Screenshot
captureScreen({
// Either png or jpg (or webm Android Only), Defaults: png
format: 'jpg',
// Quality 0.0 - 1.0 (only available for jpg)
quality: 0.8,
}).then(
//callback function to get the result URL of the screnshot
(uri) => {
setSavedImagePath(uri);
setImageURI(uri);
},
(error) => console.error('Oops, Something Went Wrong', error),
);
};
return (
<SafeAreaView style={{flex: 1}}>
<View style={styles.container}>
<Text style={styles.titleText}>
React Native Example to Take Screenshot Programmatically
</Text>
<Image
source={{uri: imageURI}}
style={{
width: 200,
height: 300,
resizeMode: 'contain',
marginTop: 5
}}
/>
<TouchableOpacity
style={styles.buttonStyle}
onPress={takeScreenShot}>
<Text style={styles.buttonTextStyle}>
Take Screenshot
</Text>
</TouchableOpacity>
<Text style={styles.textStyle}>
{
savedImagePath ?
`Saved Image Path\n ${savedImagePath}` : ''
}
</Text>
</View>
</SafeAreaView>
);
};
export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
padding: 10,
alignItems: 'center',
},
titleText: {
fontSize: 22,
textAlign: 'center',
fontWeight: 'bold',
},
textStyle: {
textAlign: 'center',
padding: 10,
},
buttonStyle: {
fontSize: 16,
color: 'white',
backgroundColor: 'green',
padding: 5,
minWidth: 250,
},
buttonTextStyle: {
padding: 5,
color: 'white',
textAlign: 'center',
},
});
To Run the React Native App
Open the terminal again and jump into your project using.
cd ProjectName
1. Start Metro Bundler
First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command:
npx react-native start
Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.
2. Start React Native Application
To run the project on an Android Virtual Device or on real debugging device:
npx react-native run-android
or on the iOS Simulator by running (macOS only)
npx react-native run-ios
Output Screenshots
Android
IOS
This is how you can take a screenshot programmatically. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!
Hope you liked it. 🙂
Hello, can i save screenshot in my gallary??
You can just use CameraRoll like this:
See savetocameraroll
hlo brother , i am doing same thing and All permission are given by me but it every time show Permission denied.
please help me.
Are trying on Android or iOS? Can you please share the same on aboutreact11@gmail.com ?
Hello,
Is there any way to capture particular part?
It does support this, and that’s it’s main goal actually.
Now, there are a couple of ways to do it: declaratively, and programmatically. Check the docs:
https://github.com/gre/react-native-view-shot
Fir example there are declarative. The use a component, which receives a `ref` and some options. And then, you trigger the capture on component mount.
For me, the programmatic fashion is better. I don’t want to trigger a capture when component mounts. So, I use `captureRef` method. The code it’s quite similar to the one described in this article (`captureScreen`).
Check the official docs, or read this answer here: https://stackoverflow.com/a/57532342/3001897
Good luck.
@Guillermo, Thanks for your valuable suggestions 🙂