Skip to content

Instantly share code, notes, and snippets.

@yasalmasri
Created July 1, 2016 11:37
Show Gist options
  • Save yasalmasri/d6e665b1c2d3e863ab46241cb61f7439 to your computer and use it in GitHub Desktop.
Save yasalmasri/d6e665b1c2d3e863ab46241cb61f7439 to your computer and use it in GitHub Desktop.
Image Download Extension
extension UIImageView {
public func imageFromUrl(urlString: String) {
if let url = NSURL(string: urlString) {
let request = NSURLRequest(URL: url)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
if let imageData = data as NSData? {
self.image = UIImage(data: imageData)
}
});
// do whatever you need with the task e.g. run
task.resume()
}
}
}
extension UIImageView {
func downloadImageFrom(link link:String, contentMode: UIViewContentMode) {
NSURLSession.sharedSession().dataTaskWithURL( NSURL(string:link)!, completionHandler: {
(data, response, error) -> Void in
dispatch_async(dispatch_get_main_queue()) {
self.contentMode = contentMode
if let data = data { self.image = UIImage(data: data) }
}
}).resume()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment