ikhaliq15 ikhaliq15 - 11 months ago 135
Swift Question

Mac Swift - How do I download an image and load it into a NSImageView?

I want to download an image from a URL and show it in my swift application for OSX. The solutions for iOS do not work for OSX.

I want the image to get downloaded and show it in a NSImageView.

Image is here if curious..

Thanks for the help ahead of time.


By simply:

let image = NSImage(byReferencingURL:NSURL(string: "http://theinterweb.net/ico/robotics_notes.gif")!)
let imageView = NSImageView()
imageView.image = image

You can get the Playground Example here, or in Objective-C:

NSImage *image = [[NSImage alloc] initByReferencingURL:[NSURL URLWithString:@"http://theinterweb.net/ico/robotics_notes.gif"]];
NSImageView *imageView = [[NSImageView alloc] init];
imageView.image = image;

You can, of course, load the URL into NSData then convert to NSImage if you like, just like on iOS.