React-Native Populate image with URL that has been converted from a blob

£可爱£侵袭症+ 提交于 2020-01-11 04:06:45

问题


I am trying to populate an image with a URl

<Image source={{uri: this.state.imageURL}} style={styles.thumb} />

I request the image from the server, using the fetch API, and it returns a blob.

I then convert the BLOB into a URL using the following line:

var imageURL = window.URL.createObjectURL(attachmentBLOB);

When I console.log() the imageURL it prints:

blob:http%3A//localhost%3A8081/4ce24d92-0b7e-4350-9a18-83b74bed6f87

I am getting no errors or warning. The image is just not displaying

I am using the android emulator.

Please help. Thanks in advance!


回答1:


Solution

React-Native does not support blobs [ref: Git/React-Native]. In order to get this working I had to download react-native-fetch-blob which returns a base64 string.

Function that returns base64 string:

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
I then populate the image withe the returned base64Image with:

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


来源:https://stackoverflow.com/questions/38370189/react-native-populate-image-with-url-that-has-been-converted-from-a-blob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!