Swift 3 : URL Image makes UITableView scroll slow issue

前端 未结 4 778
死守一世寂寞
死守一世寂寞 2021-01-06 21:25

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

4条回答
  •  长情又很酷
    2021-01-06 21:36

    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.

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

    Example of Kingfisher usage:

    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
    }
    

    Hope it helps

提交回复
热议问题