Created
November 3, 2015 09:55
-
-
Save wujianguo/4b82baa9bc0576df2faf to your computer and use it in GitHub Desktop.
UIImageView set image with url
This file contains hidden or 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 { | |
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