【问题标题】:NSCache Not Working Properly , Not Able To Load Images From CacheNSCache 无法正常工作,无法从缓存中加载图像
【发布时间】:2018-09-25 06:25:36
【问题描述】:

由于 NSCache 已更改为泛型类型 NSCache 现在这是我正在执行的代码 但是,控制台正在打印-“在图像缓存之后”和调度主队列CustomImageView,但从缓存执行的图像没有显示在那里!也许它没有存储在我正在创建的缓存中!有什么帮助吗?将不胜感激

let imageCache = NSCache<AnyObject, AnyObject>()

类 CustomImageView: UIImageView { var imageURLString : 字符串?

func loadImageUsingURlString(urlString: String)
{
    imageURLString = urlString

    let url = URL(string: urlString)

    image = nil
    if let imageFromCache = imageCache.object(forKey: urlString as AnyObject ) as? UIImage
    {
        self.image = imageFromCache
        print("Image From Cache Executed")
        return
    }



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

        if error != nil
        {
            print(error ?? "loadImageUsingURlString | Class : Helpers -> CustomImageView")
            return
        }

        DispatchQueue.main.async
        {
            let imageToCache = UIImage(data: data!)
            print("After Image To Cache")


            if self.imageURLString == urlString
            {
                print("Dispatch Main Queue CustomImageView[![enter image description here][1]][1]")
                self.image = imageToCache

            }
            else
            {
                        imageCache.setObject(imageToCache!, forKey: urlString as AnyObject)
            }



        }

    }).resume()
}

}

【问题讨论】:

    标签: ios swift xcode caching networking


    【解决方案1】:

    首先:

    let imageCache = NSCache&lt;AnyObject, AnyObject&gt;() 为什么你在这里使用AnyObject 而不是你使用的类型StringUIImage。您在代码中有不必要的强制转换。

    第二个想法:

    if self.imageURLString == urlString
                {
                    print("Dispatch Main Queue CustomImageView[![enter image description here][1]][1]")
                    self.image = imageToCache
    
                }
                else
                {
                            imageCache.setObject(imageToCache!, forKey: urlString as AnyObject)
                }
    

    如果 url 匹配,您不会缓存图像。您应该删除 else 并让 imageCache.setObject(imageToCache!, forKey: urlString as AnyObject) 在异步调用后始终执行。

    顺便说一句,请始终使用更好的开发人员编写和测试过的代码:) 试试这个:Kingfisher

    【讨论】:

    • 当我不使用 else 条件时,图像会变为零
    • 你在哪里得到零?如果imageToCache 为 nil,则表示数据已损坏或异步调用失败。
    • 删除 else 后,“执行的缓存中的图像”正在打印并且图像未显示
    • 因为你有一些疯狂的选角!为什么你不使用let imageCache = NSCache&lt;String, UIImage&gt;()??
    • 抛出错误,String 不可转换为 AnyObject
    【解决方案2】:

    我认为if self.imageURLString == urlString 条件不是必需的,请您试试下面的代码,它对我来说非常有效吗?

    let imageCache = NSCache<NSString, UIImage>()
    
    extension UIImageView {
    
        func loadImageCacheWithUrlString(urlString: String) {
    
            if let cachedImage = imageCache.object(forKey: urlString as NSString){
                self.image = cachedImage
                return
            }
    
            URLSession.shared.dataTask(with: URL(string: urlString)!) { (data, response, error) in
                if error != nil {
                    return
                }
                DispatchQueue.main.async {
                    if let downloadedImage = UIImage(data: data!) {
                        imageCache.setObject(downloadedImage, forKey: urlString as NSString)
                        self.image = downloadedImage
                    }
                }
                }.resume()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-15
      • 1970-01-01
      • 1970-01-01
      • 2014-02-24
      • 2017-11-25
      • 1970-01-01
      • 2013-04-11
      相关资源
      最近更新 更多