react-native-view-shot

repository·master·Indexed 25 days ago

https://github.com/gre/react-native-view-shot

A library for capturing React Native views as images. It supports various formats and high-performance RAW modes across iOS, Android, Windows, and Web platforms. The library provides a high-level ViewShot component and a captureRef API for taking screenshots of specific components.

Tokens
8.6K
Snippets
23
Records
43
Agent score
84%

What's inside react-native-view-shot

  1. Install react-native-view-shot

    master

    Install the library using npm, Yarn, or Expo. Since React Native 0.60+, autolinking handles the linking automatically. For iOS, you must also install the CocoaPods dependencies.

    npm install react-native-view-shot
    
    # or with Yarn
    yarn add react-native-view-shot
    
    # In Expo
    npx expo install react-native-view-shot
    
    # On iOS
    npx pod-install
  2. Prerequisites for Windows Example

    master

    Before running the Windows example, ensure your development environment meets the following requirements:

    • Node: version 20 or higher
    • Visual Studio: 2022 with the Universal Windows Platform development and Native desktop with C++ workloads installed
    • Windows SDK: Windows 11 SDK 10.0.22621
    • .NET: .NET 8 SDK available on your PATH
  3. Update visual regression snapshots

    master

    Visual regression snapshots for the web example are managed via Playwright. To ensure consistency across platforms (avoiding font or anti-aliasing differences), golden snapshots are generated on Linux and committed to the repository.

    Important: Do not commit snapshots generated on macOS or Windows. The command npm run test:e2e:update-snapshots is designed to fail on macOS/Windows to prevent accidental overwrites of the Linux baselines.

    1. Push your branch to trigger CI. If visual changes cause failures, CI will upload the actual screenshots as the web-snapshots-actual artifact.
    2. Download the web-snapshots-actual artifact from the failed GitHub Actions run.
    3. For each *-actual.png file, copy it to the matching baseline directory under example-web/e2e/snapshots/reference/<spec-file>.ts-snapshots/, removing the -actual suffix from the filename.
    4. Review the changes using git diff.
    5. Commit the updated baseline files if the visual changes are intended.
  4. Update Web Snapshots from CI using update-snapshots-from-ci.sh

    master

    When CI generates new snapshots for Linux (chromium-linux), you can use the update-snapshots-from-ci.sh script to integrate them into the project. This script automates downloading the web-snapshots-actual artifact, extracting it, and placing the files into the correct reference directory with the proper naming convention.

    Prerequisites

    • GitHub CLI must be installed and authenticated.
    • The CI run must have completed and successfully uploaded the web-snapshots-actual artifact.

    Usage

    1. Identify the GitHub Actions run ID from the URL of the failed CI run.
    2. Navigate to the example-web directory.
    3. Execute the script with the run ID as an argument.

    Post-update Workflow

    After the script completes, follow these steps to finalize the update:

    1. Review the changes: git diff e2e/snapshots/reference/
    2. Commit the new snapshots: git add e2e/snapshots/reference/ && git commit -m "Update Linux snapshots from CI"
    3. Push the changes to your repository.
    cd example-web
    ./scripts/update-snapshots-from-ci.sh <RUN_ID>
  5. Update Detox reference snapshots

    master

    If you have made changes that affect visual rendering, you can regenerate the reference images for either platform by setting the UPDATE_SNAPSHOTS environment variable to true before running the test command.

    iOS:

    cd example
    UPDATE_SNAPSHOTS=true npm run test:e2e:ios

    Android:

    cd example
    UPDATE_SNAPSHOTS=true npm run test:e2e:android
    cd example
    UPDATE_SNAPSHOTS=true npm run test:e2e:ios
  6. Run Automated E2E Tests with Playwright

    master

    The web example includes Playwright tests to verify app loading, navigation, PNG/JPG capture functionality, Base64 output, and image generation. You can run these tests in headless or headed mode.

    npm run test:e2e         # Run tests headless
    npm run test:e2e:headed  # Run tests with visible browser
  7. Build and run the iOS app

    master

    For iOS, you must first install CocoaPods dependencies. If you are setting up for the first time, run bundle install to install CocoaPods via Ruby bundler, then run bundle exec pod install to install the native dependencies.

    Once dependencies are installed, run the following to launch the app on an iOS Simulator or connected device:

    # Using npm
    npm run ios
    
    # OR using Yarn
    yarn ios
  8. Optimize performance with RAW and zip-base64 formats

    master

    For high-performance requirements (e.g., capturing views in < 16ms), use the raw format and zip-base64 result type on Android. This avoids expensive compression during the capture process.

    Workflow for Android:

    1. Set format: 'raw' and result: 'zip-base64'.
    2. The returned data follows the pattern width:height|base64_string.
    3. Extract dimensions and the base64 string.
    4. Use zlib.inflateSync to decompress the data.
    5. Use pngjs to convert the inflated buffer into a PNG.

    Note: Packaging PNG data is CPU intensive; consider using process.fork() for the conversion logic.

    // Required packages: npm install pngjs zlib
    const fs = require("fs");
    const zlib = require("zlib");
    const PNG = require("pngjs").PNG;
    const Buffer = require("buffer").Buffer;
    
    const format = Platform.OS === "android" ? "raw" : "png";
    const result = Platform.OS === "android" ? "zip-base64" : "base64";
    
    captureRef(this.ref, {result, format}).then(data => {
      // expected pattern 'width:height|', example: '1080:1731|'
      const resolution = /^(\d+):(\d+)\|/g.exec(data);
      const width = (resolution || ["", 0, 0])[1];
      const height = (resolution || ["", 0, 0])[2];
      const base64 = data.substr((resolution || [""])[0].length || 0);
    
      // convert from base64 to Buffer
      const buffer = Buffer.from(base64, "base64");
      // un-compress data
      const inflated = zlib.inflateSync(buffer);
      // compose PNG
      const png = new PNG({width, height});
      png.data = inflated;
      const pngData = PNG.sync.write(png);
      // save composed PNG
      fs.writeFileSync(output, pngData);
    });
  9. Run the Windows Example

    master

    To run the React Native Windows demo app for react-native-view-shot, navigate to the example-windows directory, install dependencies, autolink the Windows platform, and launch the application using the run-windows command.

    Note that run-windows performs building, deployment, and launching. If you only want to build without launching, deploying, or starting the packager, use the --no-launch --no-deploy --no-packager flags.

    cd example-windows
    npm install --legacy-peer-deps
    npx react-native autolink-windows
    npx react-native run-windows --arch x64
  10. Run E2E tests with Detox

    master

    The project includes an end-to-end (E2E) testing suite using Detox. iOS tests are production-ready and fully stable, while Android tests are functional but may experience intermittent element detection issues.

    iOS (Fully Working)

    To run iOS tests:

    cd example
    npm run test:e2e:ios

    To update iOS reference snapshots:

    cd example
    UPDATE_SNAPSHOTS=true npm run test:e2e:ios

    Android (Working with Issues)

    To run Android tests:

    cd example
    npm run test:e2e:android

    To update Android reference snapshots:

    cd example
    UPDATE_SNAPSHOTS=true npm run test:e2e:android
    cd example
    npm run test:e2e:ios