首页 > 解决方案 > 如何使用 React Native 读取以 ph:// 开头的文件作为 base64 字符串?

问题描述

我正在尝试读取ph://以 RNFS 开头的图像文件并将它们转换为 base64 字符串,但不幸的是我得到了ENOENT: no such file or directory错误。

如何读取这些文件并将它们转换为 base64?

标签: react-nativereact-native-iosreact-native-fs

解决方案


您可以使用react-native-convert-ph-asset

import RNConvertPhAsset from 'react-native-convert-ph-asset';

const convertLocalIdentifierToAssetLibrary = async (localIdentifier) => {
  return await RNHeicConverter.convert({
    // options
    path: localIdentifier,
  });
};

然后使用 react-native-fs 将其转换为 base64

 const base64 = await RNFS.readFile(newpath.path, 'base64');[enter link description here][2]

推荐阅读