【问题标题】:How to save image to Document Directory from local file in react native?如何在本机反应中将图像从本地文件保存到文档目录?
【发布时间】:2019-10-29 12:56:15
【问题描述】:

我是原生反应新手,我想将项目中本地文件夹(如 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);
      });
    }

请建议我应该如何进行? 谢谢。

【问题讨论】:

标签: react-native


【解决方案1】:

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

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);
      });

【讨论】:

  • 它给出了一个错误“无法使用本地标识符 ImageName.png 获取 PHAsset”,并且没有错误消息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-05
  • 1970-01-01
  • 1970-01-01
  • 2013-07-29
  • 1970-01-01
相关资源
最近更新 更多