1

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!!

1
  • use nsimage init with data Commented Jul 2, 2017 at 7:22

3 Answers 3

6
let image = NSImage(data: Data(contentsOf: url))
Sign up to request clarification or add additional context in comments.

2 Comments

This will block the main thread and make your app very unresponsive.
you can always use background thread for this, if it becomes unresponsive
5

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

Comments

0

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.