[英]Caching in AWS socket S3 bucket
What my requirement is "I have an application that downloads images from the amazon s3 bucket. And I need to cache those images, I used normal cache for doing it. But I need to implement the cache technique same as that of SDWebImage". 我的要求是“我有一个应用程序可以从Amazon s3存储桶下载图像。我需要缓存这些图像,我使用普通的缓存来做到这一点。但是我需要实现与SDWebImage相同的缓存技术”。 How the caching method in SDWebImage works.
SDWebImage中的缓存方法如何工作。
create a cache and set image to the url string and assign it from anywhere by checking the cache have the object or not 创建一个缓存并将图像设置为url字符串,并通过检查缓存是否具有对象从任何地方分配它
let imageCache = NSCache<AnyObject, AnyObject>()
imageCache.setObject(imageToCache!, forKey: urlString as AnyObject)
if let imageFromCache = imageCache.object(forKey: urlString as AnyObject) as? UIImage {
self.image = imageFromCache
return
}
Pretty Handy UIImageview Extension for image caching. 非常方便的UIImageview扩展,用于图像缓存。
import UIKit
let imageCache = NSCache<AnyObject, AnyObject()
extension UIImageView {
func cacheImage(urlString: String){
let url = URL(string: urlString)
image = nil
if let imageFromCache = imageCache.object(forKey: urlString as AnyObject) as? UIImage {
self.image = imageFromCache
return
}
URLSession.shared.dataTask(with: url!) {
data, response, error in
if let response = data {
DispatchQueue.main.async {
let imageToCache = UIImage(data: data!)
imageCache.setObject(imageToCache!, forKey: urlString as AnyObject)
self.image = imageToCache
}
}
}.resume()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.