【问题标题】:Can not load NSImageView image from file with NSImage(byReferencingFile: )无法使用 NSImage(byReferencingFile: ) 从文件中加载 NSImageView 图像
【发布时间】:2019-08-20 05:44:17
【问题描述】:

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

当我尝试 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")
    }    
}

imageView 填满了整个窗口。

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

【问题讨论】:

  • 对 URL 使用 convenience init(byReferencing url: URL)。您是否尝试关闭 App Sandbox?
  • 谢谢,关闭应用沙箱对我有用!

标签: swift xcode macos nsimage nsimageview


【解决方案1】:

不能将桌面上的图片添加到 UIimageView,只能将图片(拖动)添加到项目文件夹中,然后在 UIimageView 属性(检查器)中选择图片名称。

【讨论】:

【解决方案2】:

要访问 UserDir,您必须使用:

let home = FileManager.default.homeDirectoryForCurrentUser

然后您可以从那里添加图像的路径 这是使用文件系统的教程 https://www.raywenderlich.com/666-filemanager-class-tutorial-for-macos-getting-started-with-the-file-system

希望这会有所帮助。

【讨论】:

    【解决方案3】:

    Willeke 帮助我找到了解决方案。我不得不将 entitlements-file 中的 App SandboxYES 切换到 NO

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-07
      • 2021-05-19
      • 1970-01-01
      相关资源
      最近更新 更多