无法使用 NSImage(byReferencingFile:) 从文件加载 NSImageView 图像

Joz*_*ott 0 macos xcode nsimage nsimageview swift

当我想将我的光盘 ( /Users/me/Desktop/image.png ) 中的图像设置为我的图像时NSImageViewNSImage(byReferencingFile: )它不会显示。

当我尝试NSImage(named: )使用存储在assets-folder 中的图像时,它可以工作。

viewController 中的代码

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

ImageView的填满整个窗口。

所以我想加载存储在磁盘或指定 URL 上的图像。

Joz*_*ott 5

Willeke帮助我找到了解决方案。我不得不切换App Sandbox权利文件YESNO