简体   繁体   English

React-native - 使用已转换为URL的Blob填充图像

[英]React-native - Populate image with Blob that has been converted to a URL

I want to populate an image with a uri. 我想用uri填充图像。
I request the image from the server and it returns a BLOB. 我从服务器请求图像,它返回一个BLOB。

BLOB when displayed to console: 显示到控制台时BLOB: 在此输入图像描述

I then convert the BLOB into a URL with the following line: 然后我将BLOB转换为具有以下行的URL:

var blobUrl = URL.createObjectURL(blob);  

blobUrl when displayed to console blobUrl显示到控制台时 在此输入图像描述

I then try and populate the Image with the URL: 然后我尝试使用URL填充图像:

<Image source={{uri: blobURL}} style={{width: 100, height: 50}} />

The image will not display. 图像不会显示。 What should I do? 我该怎么办?

I am using the android emulator which is connected to the localhost. 我正在使用连接到localhost的android模拟器。 Could possibly have something to do with that seen as the BLOB url would be stored to the localhost? 可能与BLOB url存储到localhost的情况有关吗?

Or it could be a simple syntax error? 或者它可能是一个简单的语法错误?

Thanks. 谢谢。

Solution

React-Native does not support blobs [ref: Git/React-Native ]. React-Native不支持blob [ref: Git / React-Native ]。 In order to get this working I had to download react-native-fetch-blob which returns a base64 string. 为了实现这一点,我必须下载react-native-fetch-blob ,它返回一个base64字符串。

Function that returns base64 string: 返回base64字符串的函数:

var RNFetchBlob = require('react-native-fetch-blob').default;

getImageAttachment: function(uri_attachment, mimetype_attachment) {

  return new Promise((RESOLVE, REJECT) => {

    // Fetch attachment
    RNFetchBlob.fetch('GET', config.apiRoot+'/app/'+uri_attachment)
      .then((response) => {

        let base64Str = response.data;
        var imageBase64 = 'data:'+mimetype_attachment+';base64,'+base64Str;
        // Return base64 image
        RESOLVE(imageBase64)
     })

   }).catch((error) => {
   // error handling
   console.log("Error: ", error)
 });
},

Populate Image with base64 使用base64填充图像
I then populate the image withe the returned base64Image with: 然后我用返回的base64Image填充图像:

<Image source={{uri: imageBase64}} style={styles.image} />

After you have received the blob: 收到blob后:

let imageUri = "data:image/png;base64," + blob;

<Image source={{uri: imageUri, scale: 1}} style={{height: 30, width: 30}}/>

可以通过react-native-fetch-blob来解决,这是关于问题#854

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM