react native 截图并保存到相册

首先需要三个包

  1. react-native-view-shot (截图,将图片保存到临时路径)
  2. react-native-fs (更改图片路径,从临时路径移出来)
  3. react-native-camera-roll/camera-roll (将图片保存到相册)

直接上代码

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是一个持久路径。根据需要使用对应的路径地址

相关推荐

  1. react native 保存相册

    2024-07-18 18:56:08       21 阅读
  2. C# 保存为图片

    2024-07-18 18:56:08       32 阅读
  3. unity(WebGL) 把保存下载本地

    2024-07-18 18:56:08       55 阅读
  4. Android保存图片系统通知系统相册刷新

    2024-07-18 18:56:08       52 阅读
  5. unity 相机

    2024-07-18 18:56:08       45 阅读
  6. electron 视频抓保存图片本地

    2024-07-18 18:56:08       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-18 18:56:08       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-18 18:56:08       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-18 18:56:08       58 阅读
  4. Python语言-面向对象

    2024-07-18 18:56:08       69 阅读

热门阅读

  1. MySQL入门学习-深入索引.函数和表达式索引

    2024-07-18 18:56:08       24 阅读
  2. 超撒加密2.0

    2024-07-18 18:56:08       27 阅读
  3. P1009 [NOIP1998 普及组] 阶乘之和

    2024-07-18 18:56:08       22 阅读
  4. Springboot加载机制

    2024-07-18 18:56:08       18 阅读
  5. 100亿美元,得物估值到顶了吗?

    2024-07-18 18:56:08       21 阅读
  6. 3 WebAPI

    3 WebAPI

    2024-07-18 18:56:08      20 阅读
  7. Python--文件读写

    2024-07-18 18:56:08       22 阅读
  8. 【人工智能】生成式AI的未来发展方向探讨

    2024-07-18 18:56:08       21 阅读
  9. C语言 goto语句

    2024-07-18 18:56:08       19 阅读
  10. llama-cpp-python

    2024-07-18 18:56:08       21 阅读
  11. sqlalchemy定期保持mysql连接活跃

    2024-07-18 18:56:08       19 阅读