Skip to content

Instantly share code, notes, and snippets.

@muzahid59
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save muzahid59/b1c4234d6e4b7972cd9c to your computer and use it in GitHub Desktop.

Select an option

Save muzahid59/b1c4234d6e4b7972cd9c to your computer and use it in GitHub Desktop.
Swift Download image asynchronously with completion handler
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