我从URL
获取图片并将图片设置为UIImageView
并缓存字典。
但是当我将图像设置为字典时,我收到了这个错误:swift 3.0对成员'下标'的模糊引用。
var imageCache = Dictionary<String, UIImage>()
func getImage(url: String, imageView: UIImageView) {
let image = self.imageCache[url] as UIImage?
if image == nil {
let url = URL(string:url)!
let req = NSMutableURLRequest(url:url)
let session = URLSession.shared
let task = session.dataTask(with: req as URLRequest) { (data, response, error) in
if data != nil && error == nil {
let image: UIImage = UIImage(data: data!)!
imageView.image = image
self.imageCache[url] = image //******* Here got the error!
}
}
task.resume()
} else {
imageView.image = image
}
}
感谢您的帮助。
答案 0 :(得分:0)
您正在使用密钥URL
,它无效,将其更改为url.absoluteString
,因为您将字典键声明为字符串