Created
October 19, 2014 19:28
-
-
Save toast38coza/df02015417f5ff542eaa to your computer and use it in GitHub Desktop.
Save an image downloaded from the web
This file contains 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
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