如何在react native中将base64图像数据转换为数据URL?

cu6pst1q  于 2023-02-13  发布在  React
关注(0)|答案(2)|浏览(248)

我正在使用react-native-signature-canvas,它将签名作为base64图像字符串返回。我想将其发送到API,在此之前,我想将其转换为数据URL。有人能帮助我吗?另外,我不想将文件/图像存储到本地存储器。我只想将其转换为DataUrl并发送到API。

mjqavswn

mjqavswn1#

尝试以下代码将base64ImageString转换为DataUrl

const DataUrl = `data:image/png;base64,${base64ImageString}`;

参考:Upload base64 Image to Firebase React native

axr492tv

axr492tv2#

1.安装react-native-fs

import RNFS from 'react-native-fs';
import {Platform} from 'react-native';

  const createTempImage = async base64String => {
    try {

      let base64 = base64String.replace('data:image/png;base64,', '');
      const fileName = `${Date.now()}.png`;
      
      // creates a file in temporary directory to delete later
      const path = `${RNFS.TemporaryDirectoryPath}/${fileName}`;
      await RNFS.writeFile(path, base64, 'base64');
      
      const image = {
        uri: Platform.OS == 'ios'? path: 'file://' + path,
        name: fileName,
        type: 'image/png',
      };

     return image
    } catch (error) {
      console.log(error);
    }
  };

1.您可以删除文件时,请求完成

相关问题