Cache failing to save tableView cell image

112 Views Asked by At

I'm trying to save images in cache so that when I scroll up and down it doesn't have to download these images again. Unfortunately this doesn't seem to work because after all the images are downloaded when I scroll network activity goes up. Any ideas?

 let cache = NSCache<NSString, UIImage>()

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: idForCell, for: indexPath)

        let usr = userList[indexPath.row]
        cell.textLabel?.text = usr.name
        cell.detailTextLabel?.text = usr.email

        if cell.imageView?.image != nil{
            return cell
        }

        let key = NSString(string: usr.imgUrl!)

        if cache.object(forKey: key) != nil{
            cell.imageView?.image = cache.object(forKey: key)! as UIImage
            return cell
        }

        if let imgUrl = usr.imgUrl{
            let url = NSURL(string: imgUrl)
                URLSession.shared.dataTask(with: url as! URL, completionHandler: { (data, resp, err) in
                    if err != nil{
                        print(err!)
                        return
                    }

                    DispatchQueue.main.async {
                        cell.imageView?.image = UIImage(data: data!)
                        self.cache.setObject((cell.imageView?.image)!, forKey: NSString(string: imgUrl))
                        cell.setNeedsLayout()
                    }

                }).resume()
        }
        return cell
   }
0

There are 0 best solutions below