在本机反应(Expo)中显示blob图像 [英] Display blob image in react native (Expo)

查看:22
本文介绍了在本机反应(Expo)中显示blob图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 react native 项目中有以下 fetch 函数,用于从 MS Graph 返回 blob 图像,返回有效,但我似乎无法将 blob 显示为图像.

I have the following fetch function in my react native project to return a blob image from MS Graph, the return works but i cannot seem to display the blob as an image.

        //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);
        });

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

Then i wish to display the image like so:

   <Image source={{uri: BlobImage}} style={{ height: 200, width: null, flex: 1 }}/>

推荐答案

一种方法是将您的 blob 转换为 base64 并将其用作 uri,如所述 这里但我宁愿使用 rn-fetch-blob 并使用路径因为更直接向前.检查这个例子:

One way is to convert your blob into base64 and use it as uri as described here But I would rather to use rn-fetch-blob and using path since is more straight forward. check this example:

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() }}/>
  })

这篇关于在本机反应(Expo)中显示blob图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆