Introduction and usage summary of React-Native screenshot component react-native-view-shot

Introduction and usage summary of React-Native screenshot component react-native-view-shot

1. Phenomenon

1. Requirement: Capture a certain display page and save it to the album, and share it in the form of a poster;

2. Support iOS and Android

2. Solution

1. Installation: npm i --save react-native-view-shot

2. Link processing: react-native link react-native-view-shot

3. When running on iOS, you also need to execute the following command (not required for Android): cd ios && pod install && cd ..

4. Use:

(1) References:

import { captureRef } from "react-native-view-shot";

(2) Template:

 <View ref="shareImageRef">Here is the content to be displayed</View>

(3) Methods:

// Get the intercepted image link doDownLoadImage = () => {
  captureRef(this.refs.shareImageRef, {
    format: "jpg",
    quality: 0.8
  }).then(
    uri => {

      console.error("Link is:", uri)

    },
    error => {

      console.error("Error message:", error)

    } 
  );
}

// Evolution method, get the intercepted image link and save it to the album doDownLoadImage = () => {
  captureRef(this.refs.shareImageRef, {
    format: "jpg",
    quality: 0.8
  }).then(
    uri => {

      console.error("Link is:", uri) 

      let promise = CameraRoll.saveToCameraRoll(uri);
      promise
      .then((result) => {
        alert('Save successfully!');
      })
      .catch((error) => {
        alert('Save failed!');
      });

    },
    error => {

      console.error("Error message:", error)

    } 
  );
}

Note: Save the reference (self-installation): import CameraRoll from '@react-native-community/cameraroll';

// Convert the generated link to base64 and process it into a shareable link doShareImg = () => {
  captureRef(this.refs.shareImageRef, {
    format: "jpg",
    quality: 0.8
  }).then(
    uri => {

      console.error("Link is:", uri) 

        RNFS.readFile(uri, 'base64')
      .then((content) => {

        // The shared poster image address is:

        const link = 'data:image/png;base64,' + content

        console.log("The shared poster image address is" + link)
      })
      .catch((err) => {
        console.log("reading error: " + err);
      });

    },
    error => {

      console.error("Error message:", error)

    } 
  );
}

Note: The image is converted to base64 and referenced: import RNFS from 'react-native-fs';

3. Summary:

More usage methods and parameters can be obtained as needed: https://www.npmjs.com/package/react-native-view-shot

TIPS: You may encounter such a problem on Android, as shown in the figure:

Trying to resolve view with tag 2573 which doesn't exist or

Trying to resolve view with tag 2105 which doesn't exist

I encountered both of these phenomena. The solution is to add a background color to the content that needs to be intercepted.

If you add to the module:

<View ref="shareImageRef" style={{backgroundColor: 'white'}}>Here is the content to be displayed</View>

This is the end of this article about the introduction and use of React-Native screenshot component react-native-view-shot. For more relevant React Native screenshot component content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to use SVG in React and Vue projects
  • Implementation of TypeScript in React project
  • React realizes secondary linkage (left and right linkage)
  • React.js framework Redux basic case detailed explanation
  • How to use wangEditor in vue and how to get focus by echoing data
  • Comparison of the advantages of vue3 and vue2
  • Vue realizes dynamic progress bar effect
  • Vue implements dynamic circular percentage progress bar
  • Vue realizes the percentage bar effect
  • Details on how to write react in a vue project

<<:  VMware15 installation of Deepin detailed tutorial (picture and text)

>>:  Detailed tutorial on installing MySQL 8.0.19 in zip version on win10

Recommend

VMware12 installs Ubuntu19.04 desktop version (installation tutorial)

1. Experimental description In the virtual machin...

How to enable JMX monitoring through Tomcat

Build a simulation environment: Operating system:...

Example of converting JS one-dimensional array into three-dimensional array

Today I saw a friend asking a question in the Q&a...

A brief analysis of CSS :is() and :where() coming to browsers soon

Preview versions of Safari (Technology Preview 10...

Installation tutorial of mysql 8.0.11 compressed version under win10

This article shares the installation tutorial of ...

Detailed basic operations on data tables in MySQL database

Table of contents 1. View the tables in the curre...

How to check disk usage in Linux

1. Use the df command to view the overall disk us...

How to install MySQL 8.0 database on M1 chip (picture and text)

1. Download First of all, I would like to recomme...

How does MySQL implement ACID transactions?

Preface Recently, during an interview, I was aske...

In-depth understanding of the use of Vue

Table of contents Understand the core concept of ...

Exploration of three underlying mechanisms of React global state management

Table of contents Preface props context state Sum...

Vue implements the right slide-out layer animation

This article example shares the specific code of ...

Detailed installation process of MySQL5.6.40 under CentOS7 64

MySQL5.6.40 installation process under CentOS7 64...

How to migrate sqlite to mysql script

Without further ado, I will post the code for you...

VMware Tools installation and configuration tutorial for Ubuntu

Some time ago, the blogger installed the Ubuntu s...