【亲测免费】 React Native View Shot 使用教程
2026-01-19 10:59:50作者:仰钰奇
项目介绍
react-native-view-shot 是一个用于捕获 React Native 视图并将其保存为图像的开源项目。它支持多种格式(如 PNG、JPG)和质量设置,适用于需要截图功能的应用场景。
项目快速启动
安装
首先,通过 npm 安装 react-native-view-shot:
npm install react-native-view-shot
对于 React Native 0.60 及以上版本,使用 autolink 功能:
npx pod-install
基本使用
以下是一个简单的示例,展示如何在组件挂载时捕获视图并保存为图像:
import React, { useRef, useEffect } from "react";
import ViewShot from "react-native-view-shot";
function ExampleCaptureOnMountManually() {
const ref = useRef();
useEffect(() => {
ref.current.capture().then(uri => {
console.log("do something with", uri);
});
}, []);
return (
<ViewShot ref={ref} options={{ fileName: "Your-File-Name", format: "jpg", quality: 0.9 }}>
<Text>Something to rasterize</Text>
</ViewShot>
);
}
export default ExampleCaptureOnMountManually;
应用案例和最佳实践
应用案例
- 分享功能:用户可以分享应用中的某个视图。
- 报告生成:生成包含特定数据的报告图片。
- 动态壁纸:捕获动态内容并设置为壁纸。
最佳实践
- 性能优化:避免频繁的内存分配和释放,使用可重用的图像和缓冲区。
- 错误处理:在捕获过程中处理可能的错误,如空图像或失败 Promise。
- 格式选择:根据需求选择合适的图像格式和质量。
典型生态项目
结合 react-native-svg
react-native-view-shot 可以与 react-native-svg 结合使用,捕获包含 SVG 内容的视图:
import React, { useRef, useEffect } from "react";
import ViewShot from "react-native-view-shot";
import Svg, { Circle } from "react-native-svg";
function ExampleCaptureSVG() {
const ref = useRef();
useEffect(() => {
ref.current.capture().then(uri => {
console.log("do something with", uri);
});
}, []);
return (
<ViewShot ref={ref} options={{ fileName: "Your-File-Name", format: "png", quality: 1 }}>
<Svg height="100" width="100">
<Circle cx="50" cy="50" r="40" stroke="black" strokeWidth="2" fill="red" />
</Svg>
</ViewShot>
);
}
export default ExampleCaptureSVG;
结合 react-native-maps
react-native-view-shot 可以与 react-native-maps 结合使用,捕获地图视图:
import React, { useRef, useEffect } from "react";
import ViewShot from "react-native-view-shot";
import MapView from "react-native-maps";
function ExampleCaptureMap() {
const ref = useRef();
useEffect(() => {
ref.current.capture().then(uri => {
console.log("do something with", uri);
});
}, []);
return (
<ViewShot ref={ref} options={{ fileName: "Your-File-Name", format: "jpg", quality: 0.9 }}>
<MapView initialRegion={{ latitude: 37.78825, longitude: -122.4324, latitudeDelta: 0.0922, longitudeDelta: 0.0421 }} />
</ViewShot>
);
}
export default ExampleCaptureMap;
通过这些示例,您可以了解如何将 `react-
登录后查看全文
热门项目推荐
相关项目推荐
atomcodeClaude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed. Get StartedRust0214
cann-learning-hubCANN 学习中心仓,支持在线互动运行、边学边练,提供教程、示例与优化方案,一站式助力昇腾开发者快速上手。Jupyter Notebook0138
uni-appA cross-platform framework using Vue.jsJavaScript08
GLM-5.2智谱开源 GLM-5.2,这是针对长文本任务的最新旗舰模型。相较于前代产品 GLM-5.1,它在长文本任务处理能力上实现了显著飞跃,并且首次在稳定的 100 万 token 上下文中提供这一能力。Jinja00
SwanLab⚡️SwanLab - an open-source, modern-design AI training tracking and visualization tool. Supports Cloud / Self-hosted use. Integrated with PyTorch / Transformers / LLaMA Factory / veRL/ Swift / Ultralytics / MMEngine / Keras etc.Python00
tiny-universe《大模型白盒子构建指南》:一个全手搓的Tiny-UniverseJupyter Notebook03
项目优选
收起
deepin linux kernel
C
32
16
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
469
465
暂无描述
Dockerfile
778
5.08 K
Ascend Extension for PyTorch
Python
758
968
本项目是CANN提供的transformer类大模型算子库,实现网络在NPU上加速计算。
C++
877
2.03 K
本项目是CANN提供的神经网络类计算算子库,实现网络在NPU上加速计算。
C++
697
1.4 K
昇腾LLM分布式训练框架
Python
185
231
JiuwenSwarm 是一款基于openJiuwen开发的智能AI Agent,它能够将大语言模型的强大能力,通过你日常使用的各类通讯应用,直接延伸至你的指尖。
Python
2.25 K
676
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
1.1 K
1.14 K
本仓库是 Flutter SDK 与 Flutter Engine 的 OpenHarmony 适配版本,由 CPF-Flutter 团队维护。开发者可使用熟悉的 Flutter 技术栈开发 OpenHarmony 应用,3.35.7 及以后的适配版本可基于本仓库源码构建支持 OpenHarmony 的 Flutter Engine。
Dart
1.04 K
271