如何在React Native中将图像从本地文件保存到文档目录?

时间:2019-10-29 12:56:15

标签: react-native

我是本机反应的新手,我想将项目中本地文件夹(例如Module / assets / ImageName.png)中的图像保存到文档目录中。我正在使用'react-native-fs'库。

代码如下:

componentWillMount() {
    var RNFS = require('react-native-fs');
    var path = RNFS.DocumentDirectoryPath + '/ImageName.png';
    // write the file
    RNFS.writeFile(path, './Module/assets/ImageName.png', 'utf8') // what to use inplace of utf8?
      .then((success) => {
        console.log(path);
      })
      .catch((err) => {
        console.log(err.message);
      });
    }

请提出我应该如何进行? 谢谢。

1 个答案:

答案 0 :(得分:0)

对于通过images包含的Xcode asset catalogs,请使用不带扩展名的图片名称:

var path = RNFS.DocumentDirectoryPath + '/ImageName.png';
RNFS.copyAssetsFileIOS('ImageName.png', path, 100, 100)
      .then(results => {
        console.log('Copy File', results);
      })
      .catch(err => {
        console.log('Error copying file', err);
      });