Swift 3:URL图像使UITableView滚动缓慢

约吉

我有一个扩展程序,可以在上打印图像URL UIImageView但是我认为问题是我tableView因为这个扩展太慢了。我想我需要为此打开线程。如何在此扩展中创建线程,或者您知道解决该问题的另一种解决方案?

我的代码:

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)
            }
        }
    }
}
弗拉德·普利切夫

我认为,这里的问题是,您需要将图像缓存在其中table view以平滑滚动。每次程序调用时,cellForRowAt indexPath它都会再次下载图像。这需要时间。

要缓存图像,您可以使用SDWebImageKingfisher

翠鸟用法示例

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
}

希望能帮助到你

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章