React Native Firebase-如何从Firebase存储中检索图像并显示在Image组件中?

时间:2018-10-26 14:19:44

标签: react-native react-native-firebase

从firebase.storage()。ref('asd')获取图像的引用后,如何在此处检索图像并分配给Image组件?

const ref = firebase.storage().ref('path/to/image.jpg');

<Image
  source={?}
/>

2 个答案:

答案 0 :(得分:1)

您可以在引用上调用getDownloadURL()来获取图像的URL,然后可以将它作为URI源传递给Image组件,例如:

const ref = firebase.storage().ref('path/to/image.jpg');
const url = await ref.getDownloadUrl();

// ... in your render

<Image
  source={{ uri: url }}
/>

另外;首次通过storage().putFile()上传图片时,结果还会返回一个downloadUrl,可以将其存储在database / firestore实例中,以备以后使用-而不是调用{每次{1}}。

文档: screenfull.js library

答案 1 :(得分:-1)

这可能要晚了,但这对我有用。我有一个单独的存储请求,rnfirebase v6中的putFile()上没有getDownloadURL()。

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            if let dict = self.listArray[indexPath.row] as? Dictionary<String,AnyObject>
            {
                customModel = dict
                let feedModal = FeedListModal(dict)
                return self.loadimages(feedModal: feedModal, isPost: true)
        }}

    func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
            for indexPath in indexPaths {
                   print("prefetchRowsAt.row",indexPath.row)
                   if let dict = self.listArray[indexPath.row] as? Dictionary<String,AnyObject>{
                   customModel = dict
                    DispatchQueue.main.async {
                        let feedModal = FeedListModal(dict)
                       self.loadimages(feedModal:feedModal, isPost: true)
                    }
           }
       }
    }

@discardableResult func loadimages(feedModal : FeedListModal, isPost : Bool) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "PostOfferTableViewCell") as! PostOfferTableViewCell
        cell.feedPOBusinessNameLabel.text = feedModal.BusinessName
        cell.feedPOImage.sd_setImage(with: URL(string: feedModal.ImageUrl), placeholderImage: UIImage(named: "placeholder"))
        cell.businessLogoImage.sd_setImage(with: URL(string: feedModal.BusinessLogoUrl), placeholderImage: UIImage(named: "placeholder"))
        cell.selectionStyle = .none
        return cell
    }


 func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cellHeightDictionary.setObject(cell.frame.size.height, forKey: indexPath as NSCopying)
        if (indexPath as NSIndexPath).row == self.listArray.count-1
        {
             var cellHeights = [IndexPath: CGFloat]()
             cellHeights[indexPath] = cell.frame.size.height
            if let dict = self.listArray[indexPath.row] as? Dictionary<String,AnyObject>
               {
                   customModel = dict
                    DispatchQueue.main.async(execute: {
                    let feedModal = FeedListModal(dict)
                    self.loadimages(feedModal: feedModal, isPost: true)
                    })
               }
}