swiftxcodemacosnsimagensimageview

Can not load NSImageView image from file with NSImage(byReferencingFile: )


When I want to set a image from my disc (/Users/me/Desktop/image.png) to the image of my NSImageView with NSImage(byReferencingFile: ) it does not show it.

When I try NSImage(named: ) with an image stored in the assets-folder, it works.

The code within the viewController

class ViewController: NSViewController {
    @IBOutlet var imageView: NSImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        imageView.image = NSImage(byReferencingFile: "/Users/me/Desktop/image.png")
    }    
}

The imageView fills the entire window.

So I want to load the image stored on disk or at a specified URL.


Solution

  • Willeke helped me to find the solution. I had to switch App Sandbox in the entitlements-file from YES to NO