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

前端 未结 3 1467
挽巷
挽巷 2021-01-02 12:13

I want to populate an image with a uri.
I request the image from the server and it returns a BLOB.

BLOB when displayed to console:

I then convert the B

相关标签:
3条回答
  • 2021-01-02 12:42

    May be solved by react-native-fetch-blob, it's about issue #854

    0 讨论(0)
  • 2021-01-02 12:47

    After you have received the blob:

    let imageUri = "data:image/png;base64," + blob;
    
    <Image source={{uri: imageUri, scale: 1}} style={{height: 30, width: 30}}/>
    
    0 讨论(0)
  • 2021-01-02 13:00

    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} />
    
    0 讨论(0)
提交回复
热议问题