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

白昼怎懂夜的黑 提交于 2019-12-25 18:33:18

问题


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.


回答1:


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




回答2:


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.




回答3:


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).



来源:https://stackoverflow.com/questions/55414794/can-not-load-nsimageview-image-from-file-with-nsimagebyreferencingfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!