Created
February 16, 2020 19:40
-
-
Save yzhong52/b0f64356e2a9baa3db338b09423e12c8 to your computer and use it in GitHub Desktop.
Building a Client App From Scratch - Image download extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import Alamofire | |
class ImageManager { | |
private let session = Alamofire.Session() | |
static let shared = ImageManager() | |
func load(url: URLConvertible, completionHandler: @escaping (UIImage) -> Void) -> Void { | |
session.request(url).responseData( | |
queue: DispatchQueue.global(), | |
completionHandler: { response in | |
if let data = response.data, let image = UIImage(data: data) { | |
DispatchQueue.main.async { | |
completionHandler(image) | |
} | |
} | |
}) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import Alamofire | |
extension UIImageView { | |
func load(url: URLConvertible) { | |
backgroundColor = .lightGray | |
image = nil | |
ImageManager.shared.load(url: url) { [weak self] image in | |
self?.backgroundColor = nil | |
self?.image = image | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment