截图

在本教程中,了解如何使用第三方库和 Expo Media Library 捕获屏幕截图。


在本章中,我们将学习如何使用第三方库截取屏幕截图并将其保存在设备的媒体库中。我们将使用 react-native-view-shot 截取屏幕截图,并使用 expo-media-library 将图片保存在设备的媒体库中。

¥In this chapter, we'll learn how to take a screenshot using a third-party library and save it on the device's media library. We'll use react-native-view-shot to take a screenshot and expo-media-library to save an image on device's media library.

到目前为止,我们已经使用了第三方库,例如 react-native-gesture-handlerreact-native-reanimated。我们可以根据用例在 React Native 目录 上找到数百个其他第三方库。
Watch: Taking screenshots in your universal Expo app
Watch: Taking screenshots in your universal Expo app

1

Install libraries

To install react-native-view-shot and expo-media-library, run the following commands:

!!!IG3!!!

2

Prompt for permissions

An app that requires sensitive information, such as accessing a device's media library, has to prompt permission to allow or deny access. Using usePermissions() hook from expo-media-library, we can use the permission status and requestPermission() method to ask for access.

When the app loads for the first time and the permission status is neither granted nor denied, the value of the status is null. When asked for permission, a user can either grant the permission or deny it. We can add a condition to check if it is null, and if it is, trigger the requestPermission() method. After getting the access, the value of the status changes to granted.

Add the following code snippet inside the app/(tabs)/index.tsx:

!!!IG4!!!

!!!IG0!!!

3

Create a ref to save the current view

We'll use react-native-view-shot to allow the user to take a screenshot within the app. This library captures the screenshot of a <View> as an image using the captureRef() method. It returns the URI of the captured screenshot image file.

  1. Import captureRef from react-native-view-shot and useRef from React.
  2. Create an imageRef reference variable to store the reference of the screenshot image captured.
  3. Wrap the <ImageViewer> and <EmojiSticker> components inside a <View> and then pass the reference variable to it.

!!!IG5!!!

!!!IG1!!!

In the above snippet, the collapsable prop is set to false. This allows the <View> component to screenshot only of the background image and emoji sticker.

4

Capture a screenshot and save it

We can capture a screenshot of the view by calling the captureRef() method from react-native-view-shot inside the onSaveImageAsync() function. It accepts an optional argument where we can pass the width and height of the screenshot capturing area. We can read more about available options in the library's documentation.

The captureRef() method also returns a promise that fulfills with the screenshot's URI. We will pass this URI as a parameter to MediaLibrary.saveToLibraryAsync() and save the screenshot to the device's media library.

Inside app/(tabs)/index.tsx, update the onSaveImageAsync() function with the following code:

!!!IG6!!!

!!!IG2!!!

Now, choose a photo and add a sticker in the app. Then tap the "Save" button. We should see the following result on Android and iOS:

Summary

Chapter 7: Take a screenshot

We've successfully used react-native-view-shot and expo-media-library to capture a screenshot and save it on the device's library.

In the next chapter, let's learn how to handle the differences between mobile and web platforms to implement the same functionality on web.

Next: Handle platform differences