在React Native中,如何将base64图像转换为jpg,然后保存到临时路径? [英] In react native, how to convert a base64 image to jpg then save to temp path?

查看:93
本文介绍了在React Native中,如何将base64图像转换为jpg,然后保存到临时路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

react-native 中,在 react-native-fetch-blob 的帮助下,将图像转换为 BASE64 格式很容易.,我只是这样做:

In react-native, converting an image to BASE64 format is easy, with a little help of react-native-fetch-blob, I simply do:

RNFetchBlob.fs.readFile(filePath, 'base64')
    .then((data) => console.log(data));

但是,反过来呢?我知道有一些软件包可以做到这一点,但我想知道是否有可能仅使用 react-native-fetch-blob react-native-fs ?

But, what about the other way around? I know there are some packages out there that does it, but I wonder if it's possible to do it with only react-native-fetch-blob or react-native-fs?

我想要的是将 BASE64 图像转换并保存到文件后,我能够像这样显示它:

What I want is that after converting and saving the BASE64 image to a file, I am able to display it like this:

<Image
    source={{ uri: <path/to/converted/image> }}
    style={{ ... }}
/>

推荐答案

原来,这并不是一件棘手的事情,假设我有一个 BASE64 图像,如果我想将其转换为JPG ,只需像这样使用 react-native-fs :

Turns out it's not tricky at all, suppose I have a BASE64 image, if I want to convert it to a JPG, just use react-native-fs like so:

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

仅此而已

这篇关于在React Native中,如何将base64图像转换为jpg,然后保存到临时路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆