本文原创首发于公众号:ReactNative开发圈,转载需注明出处。react
React Native 截屏组件:react-native-view-shot,能够截取当前屏幕或者按照当前页面的组件来选择截取,如当前页面有一个图片组件,一个View组件,能够选择截取图片组件或者View组件。支持iOS和安卓。git
npm install react-native-view-shot react-native link react-native-view-shot
截取当前屏幕,跟系统自带的截图一致,只会截取当前屏幕显示的页面内容。若是是ScrollView,那么未显示的部分是不会被截取的。github
import { captureScreen } from "react-native-view-shot"; captureScreen({ format: "jpg", quality: 0.8 }) .then( uri => console.log("Image saved to", uri), error => console.error("Oops, snapshot failed", error) );
import { captureRef } from "react-native-view-shot"; render() { return ( <ScrollView ref="full"> <View ref="form1”> </View> <View ref="form2”> </View> </ScrollView> ); } snapshot = refname => () => captureRef(refname, { format: "jpg", quality: 0.8, result: "tmpfile", snapshotContentContainer: true }) .then( uri => console.log("Image saved to", uri), error => console.error("Oops, snapshot failed", error) );
指定须要截取的组件的ref名称,而后将该ref名称传递给snapshot方法来截取指定组件的内容。如须要截取ScrollView,只须要将”full”传递给snapshot方法便可。
captureRef方法和captureScreen方法均可以设置options,options的说明以下:
width / height:能够指定最后生成图片的宽度和高度。
format:指定生成图片的格式png or jpg or webm (Android). 默认是png。
quality:图片的质量0.0 - 1.0 (default)。
result:最后生成的类型,能够是tmpfile、base6四、data-uri。
snapshotContentContainer:若是设置为True的话,会动态计算组件的高度。若是是ScrollView组件,就会截取整个ScrollView的实际高度。web
GitHub - forrest23/ReactNativeComponents: React Native组件大全npm
GitHub - gre/react-native-view-shot: Snapshot a React Native view and save it to an imagereact-native
举手之劳关注个人微信公众号:ReactNative开发圈微信