快速,停止在表视图中加载图像

时间:2018-11-28 05:56:12

标签: swift tableview

在这里加载图像,我想在单击路径时停止加载图像。如何才能做到这一点?我尝试将URL设置为nil,但这没有用。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

            let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as? CustomCell
            let pintrestsUrl = pintrest[indexPath.row].urls?.thumb
            Library().parseImages(ImagesUrlArrayPath: pintrestsUrl!, completion: { (image) -> Void in
                if let imageFromCache = imageCache.object(forKey: pintrestsUrl as AnyObject ) as? UIImage {
                    cell?.ImageView.image = imageFromCache
                }
            })
            return cell!
        }

        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

            // stop images from loading

        }

编辑-添加了ParseImages函数

func parseImages(ImagesUrlArrayPath: String, completion: @escaping (UIImage)-> Void) {
        if let imageFromCache = imageCache.object(forKey: ImagesUrlArrayPath as AnyObject ) as? UIImage {
            completion(imageFromCache)
        }
        else
        {
        if let imageURL = URL(string: (ImagesUrlArrayPath)){
            DispatchQueue.global().async{
                let data = try? Data(contentsOf: imageURL)
                if let data = data{
                    let imageToCache = UIImage(data: data)
//                    let image = imageToCache
                    DispatchQueue.main.async {
                        imageCache.setObject(imageToCache!, forKey: ImagesUrlArrayPath as AnyObject)
                        completion(imageToCache!)
                        print("sucess")
                        //cell?.videoImageView.image = image //?.resizeImage(targetSize: size)
                        }
                    }
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

解决了一段时间 您必须先将图片设置为nil,然后再在其上加载新图片