728x90
๋ฐ˜์‘ํ˜•

Install

$ yarn add react-native-view-shot

 

Import

import ViewShot from 'react-native-view-shot';

 

Code

import ViewShot from 'react-native-view-shot';

function App() {
  const ref = useRef();

  const captureScreen = () => {
    ref.current.capture().then((uri) => {
      console.log('capture complete...', uri);
    });
  };

  return (
    <ViewShot
      style={styles.container}
      ref={ref}
      options={{ fileName: 'Your-File-Name', format: 'jpg', quality: 0.9 }}
    >
      <TouchableOpacity onPress={captureScreen}> // ํ™”๋ฉด ์บก์ฒ˜๋  ๋ถ€๋ถ„์„ ViewShot ์ปดํฌ๋„ŒํŠธ๋กœ ๊ฐ์‹ธ์ค€๋‹คโ—โ—
        <Text> ํ™”๋ฉด ์บก์ฒ˜ </Text>
      </TouchableOpacity>
    </ViewShot>
  );
}

 

๐Ÿ’ก useRef

useRef๋Š” ํŠน์ • ์ปดํฌ๋„ŒํŠธ๋ฅผ ์„ ํƒํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” hooks ํ•จ์ˆ˜์ด๋‹ค.

const ref = useRef(initialValue);

 

1๏ธโƒฃ useRef๋Š” .current ํ”„๋กœํผํ‹ฐ๋กœ ์ „๋‹ฌ๋œ ์ธ์ž(initialValue)๋กœ ์ดˆ๊ธฐํ™”๋œ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ ref ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

2๏ธโƒฃ ๋ฐ˜ํ™˜๋œ ๊ฐ์ฒด๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ์ „ ์ƒ์• ์ฃผ๊ธฐ๋ฅผ ํ†ตํ•ด ์œ ์ง€๋œ๋‹ค.

3๏ธโƒฃ ์ผ๋ฐ˜์ ์ธ ์œ ์Šค์ผ€์ด์Šค๋Š” ์ž์‹์—๊ฒŒ ๋ช…๋ น์ ์œผ๋กœ ์ ‘๊ทผํ•˜๋Š” ๊ฒฝ์šฐ์ด๋‹ค.

 

์ฐธ๊ณ  ๋ฌธํ—Œ

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

 

GitHub - gre/react-native-view-shot: Snapshot a React Native view and save it to an image

Snapshot a React Native view and save it to an image - GitHub - gre/react-native-view-shot: Snapshot a React Native view and save it to an image

github.com

 

_์„ฑํ˜ธ_