Created
July 1, 2016 11:37
-
-
Save yasalmasri/d6e665b1c2d3e863ab46241cb61f7439 to your computer and use it in GitHub Desktop.
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
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