简体   繁体   English

从Firebase异步加载图像时出错

[英]error while loading images asynchronously from firebase

I have been trying to load images asynchronously for quiet a while now. 我一直试图安静地异步加载图像。 When i make a new class for this and built and run the app an error appears in the runtime log 当我为此创建一个新类并构建并运行该应用程序时,运行时日志中会出现错误

// 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.

It is fine with extension instead of class, the images load async but, this way I will not be able to make sure the correct image is loaded in the image view. 用扩展名而不是类很好,图像异步加载,但是这样,我将无法确保在图像视图中加载了正确的图像。

//  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
}

the class i have been working with in the same view controller : 我在同一个视图控制器中使用的类:

Class to download image: 类下载图片:

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
            }
        }
    }

Why "this way" cannot make sure correct image is loaded in the image view. 为什么“通过这种方式”不能确保在图像视图中加载了正确的图像。 I read your code I think your code is fine for collectionViewController cellForItemAt 我阅读了您的代码,我认为您的代码适合collectionViewController cellForItemAt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM