Skip to content

Instantly share code, notes, and snippets.

@adamdahan
Last active October 26, 2015 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamdahan/c772d7731d4ebe733e17 to your computer and use it in GitHub Desktop.
Save adamdahan/c772d7731d4ebe733e17 to your computer and use it in GitHub Desktop.
Async image download swift
var image: UIImage?
var imageURL = "Some url"
if let url = NSURL(string: imageURL) {
let request: NSURLRequest = NSURLRequest(URL: url)
let mainQueue = NSOperationQueue.mainQueue()
NSURLConnection.sendAsynchronousRequest(request, queue: mainQueue, completionHandler: { (response, data, error) -> Void in
if error == nil {
let image = UIImage(data: data)
dispatch_async(dispatch_get_main_queue(), {
// Update image
})
}
else {
println("Error: \(error.localizedDescription)")
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment