Mac Swift - 如何下载图像并将其加载到 NSImageView 中?

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

我想从 URL 下载图像并在 OSX 的 swift 应用程序中显示它。 iOS 的解决方案不适用于 OSX。

我想要下载图像并在 NSImageView 中显示它。

简单地说:

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

你可以得到Playground Example here,或者在Objective-C:

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

当然,您可以将 URL 加载到 NSData,然后根据需要转换为 NSImage,就像在 iOS.