赞
踩
首先需要三个包
直接上代码
- import ViewShot from "react-native-view-shot";
-
- <ViewShot
- ref={viewShotRef}
- options={{ format: "jpg", quality: 0.9 }}
- >
- <View />
- </ViewShot>
这个是页面上,你要保存的截图使用ViewShot组件包括起来。
然后使用capture方法 获取截图的临时地址。
因为ios是不支持直接将临时图片保存到相册里的。
这里你还需要使用RNFS.moveFile将临时图片移动到一个持久目录下,然后再调用保存相册方法。
- const captureAndSaveScreenshot = async (viewShotRef: any) => {
- try {
- const uri = await viewShotRef.current.capture({
- format: 'jpg',
- quality: 0.9,
- result: 'tmpfile',
- snapshotContentContainer: true
- });
- const fileName = `screenshot_${Date.now()}.jpg`;
- const destPath = `${RNFS.DocumentDirectoryPath}/${fileName}`;
- console.log(destPath, 'destPath');
- await RNFS.moveFile(uri, destPath);
- const savedAsset = await CameraRoll.saveAsset(destPath);
- const photoUri = savedAsset.node.image.uri;
- return photoUri || destPath;
- } catch (error) {
- console.error('截图失败', error);
- throw error;
- }
- };

此处的photoUri是本地路径 ph:/ 开头的,destPath是一个持久路径。根据需要使用对应的路径地址
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。