Can not load NSImageView image from file with NSIm

2019-09-19 23:23发布

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.

3条回答
劳资没心,怎么记你
2楼-- · 2019-09-20 00:10

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

查看更多
迷人小祖宗
3楼-- · 2019-09-20 00:18

You can't add image from desktop to UIimageView, you only can add image (dragging) into project folders and then select the name image into UIimageView properties (inspector).

查看更多
相关推荐>>
4楼-- · 2019-09-20 00:27

To get to the UserDir you'll have to use:

let home = FileManager.default.homeDirectoryForCurrentUser

And from there you can add the path to the Image Here is a tutorial on using the File System https://www.raywenderlich.com/666-filemanager-class-tutorial-for-macos-getting-started-with-the-file-system

Hope this helps.

查看更多
登录 后发表回答