我从 URL
获取图像,并将图像设置为 UIImageView
和cache dictionary。但是,当我将图像设置为字典时,我得到了一个错误: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
}
}
感谢您的帮助。
您使用的是键为 URL
,它不起作用,将其更改为 url.absoluteString
,因为您将字典键称为字符串