【问题标题】:How to hide tableview until images are completely loaded iOS Swift?如何在图像完全加载 iOS Swift 之前隐藏 tableview?
【发布时间】:2021-05-28 09:04:17
【问题描述】:

我的图片在出现之前需要一秒钟才能加载,这看起来很糟糕。在 instagram 等应用程序上,tableview 是隐藏的,直到 tableview 加载......他们是怎么做到的?我有一个要显示的加载器,但不知道何时停止它并显示 tableview 并检测图像是否已首先完成加载?我应该把 stopTimer() 放在哪里?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell",
                                                 for: indexPath) as! MainTableViewCell
    let payment = self.payments[indexPath.row]
    cell.profilePicture.layer.cornerRadius = cell.profilePicture.frame.size.width / 2
    cell.profilePicture.clipsToBounds = true

                if let profileImageUrl = payment.picture {
                    cell.profilePicture.loadImageUsingCacheWithUrlString(profileImageUrl)
                    }
                
    if payment.message == "none" {
        cell.detailsLabel.text = "No Message"
    } else {
        cell.detailsLabel.text = "\"\(payment.message ?? "")\""
    }
}

我在 TABLEVIEW 中获取图像的代码:

let imageCache = NSCache<NSString, AnyObject>()

extension UIImageView {

func loadImageUsingCacheWithUrlString(_ urlString: String) {
    self.image = nil
    
    //check cache for image first
    if let cachedImage = imageCache.object(forKey: urlString as NSString) as? UIImage {
        self.image = cachedImage
        return
    }
    
    //otherwise fire off a new download
    guard let url = URL(string: urlString) else { return }
    URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in
        
        //download hit an error so lets return out
        if let error = error {
            print(error)
            return
        }
        
        DispatchQueue.main.async(execute: {
            
            if let downloadedImage = UIImage(data: data!) {
                imageCache.setObject(downloadedImage, forKey: urlString as NSString)
                
                self.image = downloadedImage
            }
        })
        
    }).resume()
}
}

【问题讨论】:

    标签: ios swift xcode firebase tableview


    【解决方案1】:

    您可以简单地将 SDWebImage 与 cocoaPods 一起使用,并将其用于支持缓存的异步图像下载器。您的单元格将类似于广告 SDWebImage 之后的样子。

    import SDWebImage
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell",
                                                 for: indexPath) as! MainTableViewCell
        let payment = self.payments[indexPath.row]
        cell.profilePicture.layer.cornerRadius = cell.profilePicture.frame.size.width / 2
        cell.profilePicture.clipsToBounds = true
    
        if let profileImageUrl = payment.picture {
            cell.profilePicture.sd_setImage(with: profileImageUrl, placeholderImage: UIImage(named: "yourPlaceholderImage.png"))
        }
                
        if payment.message == "none" {
        cell.detailsLabel.text = "No Message"
        } else {
        cell.detailsLabel.text = "\"\(payment.message ?? "")\""
        }
    }
    

    下载图片无需隐藏tableView。

    【讨论】:

      猜你喜欢
      • 2013-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-02
      相关资源
      最近更新 更多