Skip to content

Instantly share code, notes, and snippets.

@toast38coza
Created October 19, 2014 19:28
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 toast38coza/df02015417f5ff542eaa to your computer and use it in GitHub Desktop.
Save toast38coza/df02015417f5ff542eaa to your computer and use it in GitHub Desktop.
Save an image downloaded from the web
let url = NSURL(string: "http://www.haskell.org/happy/Happy.gif")
let urlRequest = NSURLRequest(URL: url)
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue(),
completionHandler: {
response, data, error in
if error != nil {
println("There was an error")
} else {
let image = UIImage(data: data)
var docsDirectory: String?
var paths: [AnyObject] = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
if paths.count > 0 {
docsDirectory = paths[0] as? String
var savePath = docsDirectory! + "/someImage.jpg"
NSFileManager.defaultManager().createFileAtPath(savePath, contents: data, attributes: nil)
self.someImage.image = UIImage(named: savePath)
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment