Swift 图片缓存不工作
Swift image cache not working
我正在尝试缓存来自 URL 的图像,以使我的 table 滚动更流畅。这似乎并没有缓存它们,我不知道我做错了什么。谁能告诉我这是怎么回事?
let imageCache = NSCache()
extension UIImageView {
func loadImageUsingCacheWithUrlString(urlString: String) {
self.image = nil
//check cache for image first
if let cachedImage = imageCache.objectForKey(urlString) as? UIImage {
self.image = cachedImage
return
}
//otherwise fire off a new download
let url = NSURL(string: urlString)
NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) in
//download hit an error so lets return out
if error != nil {
print(error)
return
}
dispatch_async(dispatch_get_main_queue(), {
if let downloadedImage = UIImage(data: data!) {
imageCache.setObject(downloadedImage, forKey: urlString)
self.image = downloadedImage
}
})
}).resume()
}
}
我觉得在开始新的下载之前应该有一个 else {,但即使我尝试了,我也不认为它工作正常。我什至尝试 运行 这样,滚动 table 以确保它有机会缓存所有图像,然后删除整个下载部分,这样它只会加载缓存的图像,而不会图片出现了,所以我认为它实际上并没有缓存它们。
好的,很抱歉,如果这是一个糟糕的问题。这就是最终起作用的方法:
var imageCache = NSMutableDictionary()
extension UIImageView {
func loadImageUsingCacheWithUrlString(urlString: String) {
self.image = nil
if let img = imageCache.valueForKey(urlString) as? UIImage{
self.image = img
}
else{
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(NSURL(string: urlString)!, completionHandler: { (data, response, error) -> Void in
if(error == nil){
if let img = UIImage(data: data!) {
imageCache.setValue(img, forKey: urlString) // Image saved for cache
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.image = img
})
}
}
})
task.resume()
}
}
}
我想我出了点问题什么的。
我正在尝试缓存来自 URL 的图像,以使我的 table 滚动更流畅。这似乎并没有缓存它们,我不知道我做错了什么。谁能告诉我这是怎么回事?
let imageCache = NSCache()
extension UIImageView {
func loadImageUsingCacheWithUrlString(urlString: String) {
self.image = nil
//check cache for image first
if let cachedImage = imageCache.objectForKey(urlString) as? UIImage {
self.image = cachedImage
return
}
//otherwise fire off a new download
let url = NSURL(string: urlString)
NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) in
//download hit an error so lets return out
if error != nil {
print(error)
return
}
dispatch_async(dispatch_get_main_queue(), {
if let downloadedImage = UIImage(data: data!) {
imageCache.setObject(downloadedImage, forKey: urlString)
self.image = downloadedImage
}
})
}).resume()
}
}
我觉得在开始新的下载之前应该有一个 else {,但即使我尝试了,我也不认为它工作正常。我什至尝试 运行 这样,滚动 table 以确保它有机会缓存所有图像,然后删除整个下载部分,这样它只会加载缓存的图像,而不会图片出现了,所以我认为它实际上并没有缓存它们。
好的,很抱歉,如果这是一个糟糕的问题。这就是最终起作用的方法:
var imageCache = NSMutableDictionary()
extension UIImageView {
func loadImageUsingCacheWithUrlString(urlString: String) {
self.image = nil
if let img = imageCache.valueForKey(urlString) as? UIImage{
self.image = img
}
else{
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(NSURL(string: urlString)!, completionHandler: { (data, response, error) -> Void in
if(error == nil){
if let img = UIImage(data: data!) {
imageCache.setValue(img, forKey: urlString) // Image saved for cache
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.image = img
})
}
}
})
task.resume()
}
}
}
我想我出了点问题什么的。