反应原生显示blob图像(Expo)

问题描述 投票:-1回答:1

我在我的react本机项目中有以下fetch函数从MS Graph返回一个blob图像,返回工作,但我似乎无法将blob显示为图像。

        //Blob Picture
      fetch('https://graph.microsoft.com/v1.0/me/photo/$value', {
        headers: { 'Authorization': "Bearer " + accessToken },
      })
        .then((response) => {
          // console.log(response);
               this.setState({ BlobImage: response});
        })
        .catch((error) => {
          console.error(error);
        });

然后我希望像这样显示图像:

   <Image source={{uri: BlobImage}} style={{ height: 200, width: null, flex: 1 }}/>
react-native blob microsoft-graph expo
1个回答
0
投票

一种方法是将你的blob转换为base64并将其用作描述here的uri但我宁愿使用rn-fetch-blob并使用path因为更直接。检查这个例子:

RNFetchBlob
  .config({
    fileCache : true,
    // by adding this option, the temp files will have a file extension
    appendExt : 'png'
  })
  .fetch('GET', 'http://www.example.com/file/example.zip', {
    //some headers ..
  })
  .then((res) => {
    // the temp file path with file extension `png`
    console.log('The file saved to ', res.path())
    // Beware that when using a file path as Image source on Android,
    // you must prepend "file://"" before the file path
    imageView = <Image source={{ uri : Platform.OS === 'android' ? 'file://' + res.path() : '' + res.path() }}/>
  })
© www.soinside.com 2019 - 2024. All rights reserved.