【发布时间】:2017-05-15 04:40:55
【问题描述】:
嗨,我正在 Xcode 中制作一个应用程序,并为此使用 swift。我正在从 Firebase 下载图像并在表格视图中显示它们。这有一些问题。但首先我会展示代码。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! FrontViewCell
cell.contentView.backgroundColor = UIColor.clear
//let whiteRoundedView : UIView = UIView(frame: CGRect(10, 8, self.view.frame.size.width - 20, 149))
let whiteRoundedView: UIView = UIView(frame: CGRect(x: 10, y: 8, width: self.view.frame.width - 20, height: 200))
whiteRoundedView.layer.backgroundColor = CGColor(colorSpace: CGColorSpaceCreateDeviceRGB(), components: [1.0, 1.0, 1.0, 0.8])
whiteRoundedView.layer.masksToBounds = false
whiteRoundedView.layer.cornerRadius = 2.0
whiteRoundedView.layer.shadowOffset = CGSize(width: -1, height: 1)
whiteRoundedView.layer.shadowOpacity = 0.2
cell.contentView.addSubview(whiteRoundedView)
cell.contentView.sendSubview(toBack: whiteRoundedView)
//cell.categoryImageView.image = catImages[indexPath.row]
//print("Product \(allCats[indexPath.row].name)")
cell.categoryLabel.text = allCats[indexPath.row].name
if let n = allCats[indexPath.row].name{
con?.storage?.reference(withPath: "categories/\(n).png").data(withMaxSize: 10 * 1024 * 1024, completion: {
data, error in
if error == nil{
let im = UIImage(data: data!)
cell.categoryImageView.image = im
cell.layoutSubviews()
}
else{
print("Error Downloading Image \(error?.localizedDescription)")
}
})
}
return cell
}
所以上面是将图像设置为单元格中的 imageView 的代码。 问题
- 当我向下滚动然后再次向上滚动时,相同单元格中的图像不同。
- tableview 滚动很慢。
这些都是问题。请让我知道我该如何解决这个问题? 我知道一个库 SDWebImage,但我不知道如何使用该库下载 Firebase 图像。请帮助我解决这个问题。我对这个问题感到非常疲惫。在过去的 20 个小时里,我一直试图在不睡觉的情况下解决它,但无法解决。请让我知道我做错了什么以及我应该如何解决这个问题。谢谢。
【问题讨论】:
-
这与缓存和图像的存储位置有关。您应该为此编写一个自定义类。
-
我知道这是关于兑现图像,但我不想通过编写自定义类来制造混乱。你能建议我一些 API 或类似的东西吗,这将减少我的工作量和时间。
标签: ios swift firebase sdwebimage