Loading Images via HTTP as an SKSpriteNode

For the iOS app that I am working on, I need to load images via http from a web server. In turn, these images will appear as icons within a SpriteKit scene. To accomplish this, I created an icon class that loads the image, creates an SKSpriteNode, and adds it to the scene. Loading the image is done asynchronously with the method, loadImageAsync:

func loadImageAsync(imageURL: NSURL, completionHandler: (image: UIImage!, NSError!) -> ()) {

    let request = NSMutableURLRequest(URL: imageURL)
    let session = NSURLSession.sharedSession()

    let task = session.dataTaskWithRequest(request) { data, response, error in
        guard data != nil else {
            print("No image data found: \(error)")
            return
        }
        do {
            completionHandler(image: UIImage(data: data!), error)
        }
    }
    task.resume()       
}

With this asynchronous method, after the image data is loaded, a completion handler (completionHandler) will return the image as a UIImage when the image's data has finished downloading.

Prior to calling the loadImageAsync function, I define imageURL as:

let imageURL = NSURL(string: "http://yourhost.com/image.jpg")  

In this example, imageURL is being initialized as an NSURL with the complete url pointing to the image on a web server. Continuing on, the loadImageAsync function is passed the imageURL constant and creates an SKTexture from the image once the completion handler returns the image data. Lastly, the texture is wrapped onto a sprite node, which is added to the scene:

loadImageAsync(imageURL!) { image, error in
    let imageTexture = SKTexture(image: image!)
    let imageNode = SKSpriteNode(texture: imageTexture)

    self.addChild(imageNode)
}