【问题标题】:error while loading images asynchronously from firebase从firebase异步加载图像时出错
【发布时间】:2018-07-30 01:34:06
【问题描述】:

一段时间以来,我一直在尝试异步加载图像。 当我为此创建一个新类并构建并运行应用程序时,运行时日志中会出现错误

// log
Failed to set (borderWidth) user defined inspected property on ...
could not set nil as the value for the key borderWidth.
could not set nil as the value for the key cornerRadius.

使用扩展而不是类很好,图像加载异步但是这样我将无法确保在图像视图中加载正确的图像。

//  cell for item at
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell  = collectionView.dequeueReusableCell(withReuseIdentifier: "subscribeCell", for: indexPath) as! subscribeViewCell

    cell.userID = self.users[indexPath.row].userId
    cell.profileName.text = self.users[indexPath.row].fullName
    cell.profileImage.setupWithImageUrl(imageUrl: self.users[indexPath.row].ImagePath)
    checkFollowers(indexPath : indexPath)
    return cell
}

我一直在同一个视图控制器中使用的类:

图片下载类:

private var imageCache = [String:UIImage]()

class CustomImageView : UIImageView {

    var lastImageUrl : String?

    func setupWithImageUrl (imageUrl: String) {

        self.contentMode = .scaleAspectFill
        self.clipsToBounds = true
        self.layer.cornerRadius = 14
        self.layer.borderWidth = 0

        lastImageUrl = imageUrl
        if let cachedImage = imageCache[imageUrl] {
            self.image = cachedImage
            return
        }
        guard let url = URL(string: imageUrl) else {
            return
        }
        URLSession.shared.dataTask(with: url) { (data, response, error) in
            if let error = error {
                print(error)
                return
            }
            guard let data = data else {
                return
            }
            guard let image = UIImage(data : data) else {
                return
                }
            imageCache[url.absoluteString] = image
            }
            if (url.absoluteString != self.lastImageUrl) {
            return
            }
            DispatchQueue.main.async {
                self.image = self.image
            }
        }
    }

【问题讨论】:

  • 如果我使用扩展名,我将无法声明 lastImageUrl 并保证 profileImage 中的图像是准确的。但是使用 Class 可以让我这样做。
  • 另外,如果我没有在扩展名中声明 lastImageUrl,当我向下滚动时,图像会不断变化。我正在关注这个视频来解决这个问题 (youtu.be/XFvs6eraBXM)

标签: ios swift firebase firebase-storage


【解决方案1】:

为什么“这种方式”无法确保在图像视图中加载正确的图像。我阅读了您的代码,我认为您的代码适用于 collectionViewController cellForItemAt

【讨论】:

  • 知道了。我的应用程序崩溃了,因为我没有在身份检查器中更改图像的类
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-16
  • 2011-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多