├── .gitignore ├── ImageLoader.swift ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # CocoaPods 2 | # 3 | # We recommend against adding the Pods directory to your .gitignore. However 4 | # you should judge for yourself, the pros and cons are mentioned at: 5 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control? 6 | # 7 | # Pods/ 8 | .DS_Store 9 | -------------------------------------------------------------------------------- /ImageLoader.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ImageLoader.swift 3 | // extension 4 | // 5 | // Created by Nate Lyman on 7/5/14. 6 | // Copyright (c) 2014 NateLyman.com. All rights reserved. 7 | // 8 | import UIKit 9 | import Foundation 10 | 11 | 12 | class ImageLoader { 13 | 14 | let cache = NSCache() 15 | 16 | class var sharedLoader : ImageLoader { 17 | struct Static { 18 | static let instance : ImageLoader = ImageLoader() 19 | } 20 | return Static.instance 21 | } 22 | 23 | func imageForUrl(urlString: String, completionHandler: @escaping (_ image: UIImage?, _ url: String) -> ()) { 24 | 25 | DispatchQueue.global(qos: .background).async { 26 | 27 | let data: NSData? = self.cache.object(forKey: urlString as NSString) as? NSData 28 | 29 | if let goodData = data { 30 | let image = UIImage(data: goodData as Data) 31 | DispatchQueue.main.async { 32 | completionHandler(image, urlString) 33 | } 34 | return 35 | } 36 | 37 | let session = URLSession.shared 38 | let request = URLRequest(url: URL(string: urlString)!); 39 | 40 | session.dataTask(with: request) { data, response, error in 41 | 42 | if (error != nil) { 43 | completionHandler(nil, urlString) 44 | return 45 | } 46 | 47 | if let data = data{ 48 | let image = UIImage(data: data) 49 | self.cache.setObject(data as AnyObject, forKey: urlString as NSString) 50 | DispatchQueue.main.async { 51 | completionHandler(image, urlString) 52 | } 53 | } 54 | }.resume() 55 | 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Nate Lyman 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | SwiftImageLoader 2 | ================ 3 | 4 | Asynchronous Image Loader in Swift. Caches using an NSCache. 5 | 6 | 7 | ## Usage 8 | 9 | ``` 10 | ImageLoader.sharedLoader.imageForUrl(urlString, completionHandler:{(image: UIImage?, url: String) in 11 | self.imageView.image = image 12 | }) 13 | --------------------------------------------------------------------------------