当我想将我的光盘(/Users/me/Desktop/image.png)中的图像设置为NSImageView
的NSImage(byReferencingFile: )
图像时,它不显示它。
当我尝试使用存储在assets-folder中的图像的NSImage(named: )
时,它可以工作。
viewController中的代码
class ViewController: NSViewController {
@IBOutlet var imageView: NSImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageView.image = NSImage(byReferencingFile: "/Users/me/Desktop/image.png")
}
}
imageView填充整个窗口。
所以我想加载存储在磁盘或指定URL上的图像。
要访问UserDir,您必须使用:
let home = FileManager.default.homeDirectoryForCurrentUser
从那里你可以添加图像的路径这是一个使用文件系统https://www.raywenderlich.com/666-filemanager-class-tutorial-for-macos-getting-started-with-the-file-system的教程
希望这可以帮助。
Willeke帮我找到了解决方案。我不得不在App Sandbox
的权利文件中切换YES
到NO
您无法将图像从桌面添加到UIimageView,您只能将图像(拖动)添加到项目文件夹中,然后选择名称图像到UIimageView属性(检查器)。