Snapshot Views
Creating Snapshots of Views
The function makeImageFromView
lets you take a snapshot of another React Native View as a Skia SkImage. The function accepts a ref to a native view and returns a promise that resolves to an SkImage
instance upon success.
info
It is safer to use collapsable=false
on the root view of the snapshot to prevent the root view from being removed by React Native.
If the view is optimized away, makeImageFromView
will crash or return the wrong result.
tsx
import {useState ,useRef } from "react";import {View ,Text ,PixelRatio ,StyleSheet ,Pressable } from "react-native";import type {SkImage } from "@shopify/react-native-skia";import {makeImageFromView ,Canvas ,Image } from "@shopify/react-native-skia";constpd =PixelRatio .get ();constDemo = () => {// Create a ref for the view you'd like to take a snapshot ofconstref =useRef <View >(null);// Create a state variable to store the snapshotconst [image ,setImage ] =useState <SkImage | null>(null);// Create a function to take the snapshotconstonPress = async () => {// Take the snapshot of the viewconstsnapshot = awaitmakeImageFromView (ref );setImage (snapshot );};return (<View style ={{flex : 1 }}><Pressable onPress ={onPress }><View ref ={ref }// collapsable={false} is important herecollapsable ={false}style ={{backgroundColor : "cyan",flex : 1 }}><Text >This is a React Native View</Text ></View ></Pressable >{image && (<Canvas style ={StyleSheet .absoluteFill }><Image image ={image }x ={0}y ={0}width ={image .width () /pd }height ={image .height () /pd }/></Canvas >)}</View >)};
tsx
import {useState ,useRef } from "react";import {View ,Text ,PixelRatio ,StyleSheet ,Pressable } from "react-native";import type {SkImage } from "@shopify/react-native-skia";import {makeImageFromView ,Canvas ,Image } from "@shopify/react-native-skia";constpd =PixelRatio .get ();constDemo = () => {// Create a ref for the view you'd like to take a snapshot ofconstref =useRef <View >(null);// Create a state variable to store the snapshotconst [image ,setImage ] =useState <SkImage | null>(null);// Create a function to take the snapshotconstonPress = async () => {// Take the snapshot of the viewconstsnapshot = awaitmakeImageFromView (ref );setImage (snapshot );};return (<View style ={{flex : 1 }}><Pressable onPress ={onPress }><View ref ={ref }// collapsable={false} is important herecollapsable ={false}style ={{backgroundColor : "cyan",flex : 1 }}><Text >This is a React Native View</Text ></View ></Pressable >{image && (<Canvas style ={StyleSheet .absoluteFill }><Image image ={image }x ={0}y ={0}width ={image .width () /pd }height ={image .height () /pd }/></Canvas >)}</View >)};