运行应用程序时,不会显示配置文件图像。

时间:2017-12-26 18:18:11

标签: swift3

我的代码中没有错误,但ImagevIew图像没有显示

让imageCache = NSCache()

扩展名UIImageView {

func loadImageUsingCacheWithUrlString(urlString: String) {
    //self.image = nil
    if let cachedImage = imageCache.object(forKey: urlString as AnyObject) as? UIImage {
        self.image = cachedImage
        return
    }

    let url = URL(string: urlString)
    URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in
        if error != nil {
            print(error!)
            return
        }
        DispatchQueue.main.async(execute: {
            if let currImage = UIImage(data: data!) {
                imageCache.setObject(currImage, forKey: urlString as AnyObject)
                self.image = currImage
            }
        })
    }).resume()
}

}

0 个答案:

没有答案