Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@yzhong52
Created February 16, 2020 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yzhong52/b0f64356e2a9baa3db338b09423e12c8 to your computer and use it in GitHub Desktop.
Save yzhong52/b0f64356e2a9baa3db338b09423e12c8 to your computer and use it in GitHub Desktop.
Building a Client App From Scratch - Image download extension
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)
}
}
})
}
}
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