【发布时间】:2017-09-25 04:32:10
【问题描述】:
我目前正在从我的 firebase 存储中读取图像 - 效果很好。
我已经设置了一个缓存,以便在从存储中读取图像时从缓存中读取图像:
// Storage.imageCache.object(forKey: post.imageUrl as NSString)
static func getImage(with url: String, completionHandler: @escaping (UIImage) -> ())
{
if let image = imageCache.object(forKey: url as NSString)
{
print("CACHE: Unable to read image from CACHE ")
completionHandler(image)
}
else
{
let ref = FIRStorage.storage().reference(forURL: url)
ref.data(withMaxSize: 2 * 1024 * 1024)
{
(data, error) in
if let error = error
{
print("STORAGE: Unable to read image from storage \(error)")
}
else if let data = data
{
print("STORAGE: Image read from storage")
if let image = UIImage(data: data)
{
// Caches the image
Storage.imageCache.setObject(image, forKey: url as NSString)
completionHandler(image)
}
}
}
}
}
}
但它不起作用。它似乎根本不起作用,我没有消息' print("CACHE: Unable to read image from CACHE") ' 显示在我的控制台上,但打印 ' print("STORAGE: Image read from storage") '
请问您知道这是如何实现的吗?
非常感谢您的宝贵时间!
---编辑--
我从 firebase 存储中调用表格单元格视图中的图像,然后为:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.feedTableView.dequeueReusableCell(withIdentifier: "MessageCell")! as UITableViewCell
let imageView = cell.viewWithTag(1) as! UIImageView
let titleLabel = cell.viewWithTag(2) as! UILabel
let linkLabel = cell.viewWithTag(3) as! UILabel
titleLabel.text = posts[indexPath.row].title
titleLabel.numberOfLines = 0
linkLabel.text = posts[indexPath.row].link
linkLabel.numberOfLines = 0
Storage.getImage(with: posts[indexPath.row].imageUrl){
postPic in
imageView.image = postPic
}
return cell
}
【问题讨论】:
-
这个库非常适合从 Internet 下载图像(我也将它们与 firebase 一起使用),您可以拥有进度、完成处理程序、占位符图像和出色的缓存加载:github.com/rs/SDWebImage
标签: ios swift image firebase firebase-storage