Skip to content

Instantly share code, notes, and snippets.

@wujianguo
Created November 3, 2015 09:55
Show Gist options
  • Save wujianguo/4b82baa9bc0576df2faf to your computer and use it in GitHub Desktop.
Save wujianguo/4b82baa9bc0576df2faf to your computer and use it in GitHub Desktop.
UIImageView set image with url
extension UIImageView {
func setImageWithUrlString(str: String?) {
if let url = NSURL(string: str ?? "") {
setImageWithURL(url)
}
}
func setImageWithURL(url: NSURL) {
NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) -> Void in
dispatch_async(dispatch_get_main_queue()) { () -> Void in
guard data != nil else { return }
self.image = UIImage(data: data!)
}
}.resume()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment