我最近问了这个问题: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
}
此方法的优点在于您仅在需要时加载图像,而不是主要数据集。
关于objective-c - 填充餐 table 带有firebase中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34408645/