Skip to content

Instantly share code, notes, and snippets.

@vcalfa
Created May 11, 2017 11:17
Show Gist options
  • Save vcalfa/eb59344e1890160112664c0cd05bab9b to your computer and use it in GitHub Desktop.
Save vcalfa/eb59344e1890160112664c0cd05bab9b to your computer and use it in GitHub Desktop.
UIViewImage
extension UIImageView {
func downloadedFrom(url: URL, contentMode mode: UIViewContentMode = .scaleAspectFit) {
contentMode = mode
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard
let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
let data = data, error == nil,
let image = UIImage(data: data)
else { return }
DispatchQueue.main.async() { () -> Void in
self.image = image
}
}.resume()
}
func downloadedFrom(link: String, contentMode mode: UIViewContentMode = .scaleAspectFit) {
guard let url = URL(string: link) else { return }
downloadedFrom(url: url, contentMode: mode)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment