dbrownjave
dbrownjave

Reputation: 477

How to convert URL to NSImage (Swift 3)

I need to create an NSImage from a url. I want to be able to use it in my Mac application.

enter image description here

Please Help!!

Upvotes: 1

Views: 2923

Answers (3)

dbrownjave
dbrownjave

Reputation: 477

Swift 5 and safely unwrap

do {
    if let url = URL(string: imageURL) {
        let imageData = try Data(contentsOf: url)
        let image = NSImage(data: imageData)
    }
} catch {
    // Handle the error
}

Upvotes: 0

tontonCD
tontonCD

Reputation: 358

In Swift 4.2, we can already use: let image: NSImage = NSImage(contentsOf: url!)!

Upvotes: 5

Dark Innocence
Dark Innocence

Reputation: 1389

let image = NSImage(data: Data(contentsOf: url))

Upvotes: 6

Related Questions