Last active
August 29, 2015 14:22
-
-
Save muzahid59/b1c4234d6e4b7972cd9c to your computer and use it in GitHub Desktop.
Swift Download image asynchronously with completion handler
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
| dowloadImage("link", completion: {(var image) in | |
| if image { | |
| // downloaded successfully | |
| } | |
| }) | |
| // dowload image with completion handler | |
| func dowloadImage(link: String, completion:(image: UIImage) -> Void){ | |
| dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { | |
| if let url = NSURL(string: link){ | |
| if let data = NSData(contentsOfURL: url){ | |
| if let image = UIImage(data: data){ | |
| completion(image: image) | |
| } | |
| } | |
| } | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment