问题
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