![](/img/trans.png)
[英]How can I convert a base64 url to an image(png/jpg) file, in React-Native?
[英]In react native, how to convert a base64 image to jpg then save to temp path?
在react-native
,將圖像轉換為BASE64
格式很容易,在react-native-fetch-blob
幫助下,我只是這樣做:
RNFetchBlob.fs.readFile(filePath, 'base64')
.then((data) => console.log(data));
但是,反過來呢? 我知道有一些軟件包可以做到這一點,但我想知道是否可以僅使用react-native-fetch-blob
或react-native-fs
來做到這一點?
我想要的是在將BASE64
圖像轉換並保存到文件后,我能夠像這樣顯示它:
<Image
source={{ uri: <path/to/converted/image> }}
style={{ ... }}
/>
事實證明它根本不棘手,假設我有一個BASE64
圖像,如果我想將其轉換為JPG
,只需像這樣使用react-native-fs
:
import RNFS from 'react-native-fs';
const imageDate = '<some base64 data>';
const imagePath = `${RNFS.TemporaryDirectoryPath}image.jpg`;
RNFS.writeFile(imagePath, imageData, 'base64')
.then(() => console.log('Image converted to jpg and saved at ' + imagePath));
僅此而已
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.