首次加载时 NSCache 不适用于所有图像 [英] NSCache Doesn't work with all images when loading for the first time

查看:10
本文介绍了首次加载时 NSCache 不适用于所有图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 swift 3.0 中进行一个项目,在该项目中,我使用 NSCache 缓存来自服务器的响应,并将它们填充到 UITableView 中.但是由于某种原因,当应用程序第一次加载时,我只看到很少的图像加载,但是如果我滚动并返回,我会看到所有内容(从服务器检索响应结束,我也重新加载了我的 tableview,但似乎不是这样).我不确定我在这里到底缺少什么,下面的代码显示了我如何缓存图像.

I'm woking on a project in swift 3.0 where I cache the response from the server by using NSCache as to populate them in a UITableView. However for some reason I'm only seeing few images loading when the app loads for the first time, but if If i scroll and come back I see everything (end of retrieving the response from the server I reload my tableview too, but seems that not the case). I'm not sure what I''m exactly missing here, the code as bellow as to show how I cache the images.

let imageCache = NSCache<AnyObject, AnyObject>()
var imageURLString : String?

extension UIImageView {


    public func imageFromServerURL(urlString: String) {
        imageURLString = urlString

        if let url = URL(string: urlString) {

            image = nil


            if let imageFromCache = imageCache.object(forKey: urlString as AnyObject) as? UIImage {

                self.image = imageFromCache

                return
            }

            URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in

                if error != nil{
                    print(error as Any)


                    return
                }

                DispatchQueue.main.async(execute: {

                    if let imgaeToCache = UIImage(data: data!){

                        if imageURLString == urlString {
                            self.image = imgaeToCache
                        }

                        imageCache.setObject(imgaeToCache, forKey: urlString as AnyObject)// calls when scrolling
                    }
                })
            }) .resume()
        }
    }
}

推荐答案

这里的图像正在下载并存储在缓存中就好了.问题在于tableview单元格的更新.

Here the images are downloading and stored in cache just fine. The problem lies in the updation of tableview cells.

当表格视图将单元格加载到表格时,图像尚未下载.但是一旦图像被下载,我们必须有选择地更新单元格,以便立即显示图像.

When the table view is loading the cells on to the table the images are not downloaded yet. But once the image is downloaded we have to selectively update the cell so that the image is displayed instantly.

由于您正在滚动,tableview 再次调用cellForRowatIndexpath",这会在滚动时更新显示下载图像的单元格.

Since you are scrolling , the tableview calls 'cellForRowatIndexpath' again which updates the cell showing the downloaded images while scrolling.

如果您仍然希望使用扩展,我建议您添加 tableView 和 indexpath 作为参数,以便我们可以调用重新加载特定行并立即更新视图.

If you still wish to use the extension , I suggest you add the tableView and indexpath as the parameters so that we can call reload specific row and have the view updated instantly.

我已经更新了扩展中定义的函数的表重载代码和结构.让我知道进展如何.

I have updated the table reload code and structure of the function defined in extension. Let me know how it goes.

let imageCache = NSCache<AnyObject, AnyObject>()
var imageURLString : String?

extension UIImageView {


public func imageFromServerURL(urlString: String, tableView : UITableView, indexpath : IndexPath)) {
    imageURLString = urlString

    if let url = URL(string: urlString) {

        image = nil


        if let imageFromCache = imageCache.object(forKey: urlString as AnyObject) as? UIImage {

            self.image = imageFromCache

            return
        }

        URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in

            if error != nil{
                print(error as Any)


                return
            }

            DispatchQueue.main.async(execute: {

                if let imgaeToCache = UIImage(data: data!){

                    if imageURLString == urlString {
                        self.image = imgaeToCache
                    }

                    imageCache.setObject(imgaeToCache, forKey: urlString as AnyObject)// calls when scrolling

    tableView.reloadRows(at: [indexpath], with: .automatic)

                }
            })
        }) .resume()
    }
}

这篇关于首次加载时 NSCache 不适用于所有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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