将存储为base64的JSON图像与React-Native中的其他数据一起渲染

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

I have a JSON file that my app retrieves from the api

It contains name location and other information along with an image in b ase64 encoding

[ { meta: { views: 0, tapped: 0, favs: 0, priority: 0 },
    img: { data: [Binary] },
    special: true,
    _id: 5cace9fbeb520a4d8833bc1b,
    date: 2019-04-09T00:00:00.000Z,
    title: 'Test',
    url: 'http://goole.com',
    body: 'Oh it chaned',
    published: false,
    category: 'Sports',
    location: 'dafasfasf',
    __v: 0,
    publishDate: 2019-04-09T18:30:00.000Z,
    subCategory: 'Footabll' },
  { meta: { views: 0, tapped: 0, favs: 0, priority: 0 },
    img: { data: [Binary] },
    special: true,
    _id: 5cacf8e54b578827e8425c83,
    date: 2019-04-02T00:00:00.000Z,
    title: 'Testasd',
    url: 'http://google.com',
    body: 'wadwadwad',
    published: false,
    category: 'Sports',
    location: 'Random',
    subCategory: 'Footabll',
    __v: 0,
    publishDate: 2019-04-09T18:30:00.000Z },

它看起来像这样

我无法以原生的方式渲染图像

我得到的对象无效作为React Child找到了带有键{data}的对象我明白这是一个对象,因此这个错误正在发生,但我无法找到解决方案(它告诉我迭代思考每个键同时我想访问特定的密钥)。

这是我在渲染中调用的函数

viewfunc(){



return(
  this.state.dataSource.map((item)=>{

    return(

    < View key={Math.random()} style={styles.slide3}>

      <Text style={styles.text}>{item.title}</Text>
      <Image
      style={{
        width: 51,
        height: 51,
        resizeMode: 'contain',
      }}
      source={{
        uri:
          'data:image/png;base64,${item.img.data}',
      }}
    />
      <Text style={styles.body}>{item.body}</Text>

    </View>)

}
))}
))}

我无法渲染这个img,如果我尝试通过在文本字段中放置{item.img.data}来打印base64它返回的对象无效..错误我能够访问带有{item.meta.views}的元请我需要帮助!

编辑

这是我在将图像推送到数据库时对其进行编码的方式

var newImg = fs.readFileSync(req.file.path);
var encImg = newImg.toString('base64');

也试过这个

  <Image
        style={{
          width: 51,
          height: 51,

        }}
        source={{
          uri:
            `data:image/png;base64,`+item.img.data,
        }}
      />

仍然没有渲染..

android node.js reactjs react-native react-native-android
2个回答
1
投票

使用带反引号的字符串插值:

`data:image/png;base64,${item.img.data}`

-1
投票

尝试类似的东西

     <Image source={ uri: `data:${image.mime};base64,` + image.data, width: image.width, height: image.height }
/>
© www.soinside.com 2019 - 2024. All rights reserved.