Swift 3:URL Image使UITableView滚动缓慢问题 [英] Swift 3 : URL Image makes UITableView scroll slow issue

查看:181
本文介绍了Swift 3:URL Image使UITableView滚动缓慢问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 UIImageView 上打印图片网址的扩展程序。但我认为问题是我的 tableView 因为这个扩展而太慢了。我想我需要为它打开线程。如何在此扩展程序中创建一个主题,或者您是否知道解决此问题的其他解决方案?

I have an extension to print image URL on UIImageView. But I think the problem is my tableView is so slow because of this extension. I think I need to open thread for it. How can I create a thread in this extension or do you know another solution to solve this problem?

我的代码:

extension UIImageView{

    func setImageFromURl(stringImageUrl url: String){

        if let url = NSURL(string: url) {
            if let data = NSData(contentsOf: url as URL) {
                self.image = UIImage(data: data as Data)
            }
        }
    }
}


推荐答案

我认为,这里的问题是,你需要在表视图中缓存你的图像才能顺利滚动。每次程序调用 cellForRowAt indexPath 时,它都会再次下载图像。这需要时间。

I think, that problem here, that you need to cache your images in table view to have smooth scrolling. Every time your program calls cellForRowAt indexPath it downloads images again. It takes time.

对于缓存图像,您可以使用 SDWebImage 等库, Kingfisher 等。

For caching images you can use libraries like SDWebImage, Kingfisher etc.

翠鸟用法:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "identifier", for: indexPath) as! CustomCell

    cell.yourImageView.kf.setImage(with: URL) 
    // next time, when you will use image with this URL, it will be taken from cache.

    //... other code
}

希望它有帮助

这篇关于Swift 3:URL Image使UITableView滚动缓慢问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆