我正在使用react-native-signature-canvas,它将签名作为base64图像字符串返回。我想将其发送到API,在此之前,我想将其转换为数据URL。有人能帮助我吗?另外,我不想将文件/图像存储到本地存储器。我只想将其转换为DataUrl并发送到API。
mjqavswn1#
尝试以下代码将base64ImageString转换为DataUrl
base64ImageString
DataUrl
const DataUrl = `data:image/png;base64,${base64ImageString}`;
参考:Upload base64 Image to Firebase React native
axr492tv2#
1.安装react-native-fs
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.您可以删除文件时,请求完成
2条答案
按热度按时间mjqavswn1#
尝试以下代码将
base64ImageString
转换为DataUrl
参考:Upload base64 Image to Firebase React native
axr492tv2#
1.安装
react-native-fs
1.您可以删除文件时,请求完成