objective-c - 填充餐 table 带有firebase中的图像

标签 objective-c uitableview firebase data-retrieval

我最近问了这个问题:Populating a UITableView in Firebase a Set Number of Cells at a time

但是通过讨论我意识到这个问题实际上是两个问题,所以我将为第二个问题启动第二个线程:

人们是否有办法使用 Firebase 从大型数据集中填充图像的表格 View ?

下面是我用于检索图像的代码。图像是 base64 编码的 NSString。

NSString* profPicString= [child.value objectForKey: @"profilePicture"];
    NSData *dataFromBase64=[NSData base64DataFromString:profPicString];
    UIImage *profPicImage = [[UIImage alloc]initWithData:dataFromBase64];
    item.profilePicture = profPicImage;

但是,下载图像需要很长时间。仅包含 10 张图像的表格 View 将需要 5 或 10 秒才能加载。如果像这样有 10 张图像,那么如果有数百或数千张图像,那就真的是冰川了。

我怀疑我是第一个遇到这个问题的人,我想知道是否有更好/更有效的方法来做到这一点?

感谢您的帮助!

最佳答案

如果您要将图像存储为 Base64 编码字符串,请将它们存储在 Firebase 数据库中的单独位置。

{
   "pokemon": {
      "001": {
         "name": "Bulbasaur"
      },
      "002": {
         "name": "IvySaur"
      }
   },
   "images": {
      "001": "big-ole-base64-string",
      "002": "big-ole-base64-string"
   }
}

这样您就可以加载主要数据,而无需下拉图像。

现在,当您为 UITableView 加载数据时,为主要数据集创建一个监听器,在本例中为 /pokemon

然后,当您到达 tableView(_:cellForRowAtIndexPath) 时,创建一个一次性监听器来抓取图像。

此示例是用 Swift 编写的,但您可以转换为 Objective-C。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  let cell = tableView.dequeueReusableCellWithIdentifier("PokeCell") as! PokeTableViewCell

  let pokeSnap = pokemonSnaps[indexPath.item]

  let imageRef = rootRef.childByAppendingPath("images").childByAppendingPath(pokeSnap.key)

  imageRef.observeSingleEventOfType(.Value) { (imageSnap: FDataSnapshot!) in
    let base64String = imageSnap.value as! String
    let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions(rawValue: 0))
    let image = UIImage(data: decodedData!)
    cell.pokeImage.image = image
  }

  return cell   
}

此方法的优点在于您仅在需要时加载图像,而不是主要数据集。

Check out this repo of mine that implements this technique.

关于objective-c - 填充餐 table 带有firebase中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34408645/

相关文章:

angularfire2 - 如何在 firebase 中使用多个 where 子句?

Objective-C:@[]和[[NSArray alloc] initWithCapacity:0]有什么区别

iphone - 表格 View 中的事件指示器

ios - UITableViewCell 和 AutoLayout 打破约束的两种状态

ios - UITableView 禁用某些行的 rowEditActionForRowAtIndexPath

ios - Tableview 单元格大小没有改变? swift

android - 在 Firebase 查询开始执行之前进行大量 GarbageCollector 调用

angularjs - 当对象包含 ng-repetate 时,如何使用 angularFire 保存 Firebase 对象 $asArray()

objective-c - 如何声明具有未确定返回类型的 C 函数?

iphone - 使用 playersToInvite 在非 friend 之间进行游戏中心配对