【问题标题】:Load and return an image from Firebase database/storage in React Native在 React Native 中从 Firebase 数据库/存储加载并返回图像
【发布时间】:2017-03-06 10:12:55
【问题描述】:

我有一个 Firebase 应用设置,在实时数据库中有一组项目。每个项目都有一个 imagePath 节点,其中包含一个 Firebase 存储 URI(例如 gs://bucket/images/stars.jpg)。

项目数组用于<ListView/>,我需要在每一行中加载一个图像。

由于图像不是直接以“普通” URL 格式存储的,我需要使用存储 API 来获取它。

所以在我的renderRow(responseItem) 函数中,我有以下内容:

<View style={styles.imgWrap}>
    {this.getImage(responseItem.imgPath)}
</View>

那个函数如下:

getImage(path) {
    FirebaseApp.storage().refFromURL(path).getDownloadURL().then((url) => {

        return (
            <Image
                style={styles.candidateImg}
                source={{uri: url}}/>
        )

    })

}

问题是,没有返回任何东西。

如果我在getImage 中的return 上方运行console.log(url),它会记录正确的URL,我可以在浏览器中打开。

Tldr; storage API 正确获取图像 URL,但 react-native 未返回 &lt;Image/&gt; 组件。

【问题讨论】:

    标签: javascript firebase react-native firebase-storage


    【解决方案1】:

    我认为问题在于异步调用getImage(),试试这个:

    <View style={styles.imgWrap}>
        <Image
            style={styles.candidateImg}
            source={this.state.img}/>
    </View>
    
    
    getImage(path) {
        FirebaseApp.storage().refFromURL(path).getDownloadURL().then((url) => {
            this.setState({img: {uri: url}});
        })
    }
    
    // and invoke in renderRow
    this.getImage(responseItem.imgPath)
    

    当然你应该创建一个uri数组来处理&lt;ListView /&gt;

    【讨论】:

    • 啊哈,所以我会将this.state.img 作为一个数组,将每行图像添加到其中,然后在每行中获取必要的数组项?像source={this.state.img[rowId]}.
    猜你喜欢
    • 2017-01-16
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    相关资源
    最近更新 更多