Create a new swift file and copy paste the below class
It also store images in cache and when you load the image next time with this class it will be load quickly.
import UIKit
class ImageLoader {
var cache = NSCache<AnyObject, AnyObject>()
class var sharedInstance : ImageLoader {
struct Static {
static let instance : ImageLoader = ImageLoader()
}
return Static.instance
}
func imageForUrl(urlString: String, completionHandler:@escaping (_ image: UIImage?, _ url: String) -> ()) {
let data: NSData? = self.cache.object(forKey: urlString as AnyObject) as? NSData
if let goodData = data {
let image = UIImage(data: goodData as Data)
DispatchQueue.main.async {
completionHandler(image, urlString)
}
return
}
let downloadTask: URLSessionDataTask = URLSession.shared.dataTask(with: URL.init(string: urlString)!) { (data, response, error) in
if error == nil {
if data != nil {
let image = UIImage.init(data: data!)
self.cache.setObject(data! as AnyObject, forKey: urlString as AnyObject)
DispatchQueue.main.async {
completionHandler(image, urlString)
}
}
} else {
completionHandler(nil, urlString)
}
}
downloadTask.resume()
}
}
Usage:
// Call this class for a single image or Run the below code inside a for loop and pass URL Strings in a loop to this class. Works Smoothly for both.
// Call this class for a single image or Run the below code inside a for loop and pass URL Strings in a loop to this class. Works Smoothly for both.
Wynn Casino is Closing - Dr.MCD
ReplyDeleteWynn Las Vegas is closing 군포 출장마사지 in on its sister property, Encore Boston Harbor, and 정읍 출장안마 the Wynn Las Vegas in 경주 출장안마 late July. 양산 출장샵 Wynn Boston Harbor is now closed 안성 출장샵