在React Native中将base64字符串转换为图像
问题描述:
我创建了一个具有expo's react native的社交媒体应用,并希望增加上传图片的功能.由于expo不允许您将文件转换为Blob进行上传,因此我只是将base64图像数据作为字符串上传到服务器数据库.从服务器外下载数据后,如何将这些数据再次转换为可见图像?
I created a social media app with expo's react native, and wanted to add the ability to upload images. Since expo won't let you convert a file to a blob to upload, I just uploaded the base64 image data as a string to the server database. How can I convert this data into the viewable image again after I download the data from off of the server?
答
您可以执行以下操作:
var base64Icon = 'data:image/png;base64,iVBORw0KGgoAAAANS...';
<Image style={{width: 50, height: 50}} source={{uri: base64Icon}}/>
在base64Icon
变量中,您需要将base64数据放在data:image/png;base64,
之后.像这样:
In the base64Icon
variable you need to put your base64 data after data:image/png;base64,
. Like this:
var base64Icon = 'data:image/png;base64,{PLACE_YOUR_BASE64_DATA_HERE}';