1

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.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
ikhaliq15
  • 143
  • 1
  • 12

1 Answers1

3

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.

Cai
  • 3,609
  • 2
  • 19
  • 39