【问题标题】:Is there a way to retrieve the icon image of files and folders in macOS using SwiftUI?有没有办法使用 SwiftUI 检索 macOS 中文件和文件夹的图标图像?
【发布时间】:2021-02-27 12:10:43
【问题描述】:

我正在使用 SwiftUI 在 MacOS 版本 10.15.7 中构建应用程序,我需要显示用户选择的文件/文件夹的图标。请参阅屏幕截图以了解我在这里指的是什么图标。谢谢。

Icons to the left:

Icons:

Icons to the left:

【问题讨论】:

  • 您可以在这里找到许多系统图标/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources 如果这不是您要问的,请完善您的问题。
  • 这个目录似乎包含了所有需要的图片。但是,有没有办法通过使用 URL 或 NSOpenPanel 之类的东西来引用代码中的图标图像?
  • 你可以使用NSImage imageNamed(抱歉不知道swiftui所以不确定确切的语法)有很多操作系统映像使用NSImageName类型。见developer.apple.com/documentation/appkit/nsimagename
  • 非常感谢!这行得通。

标签: macos image swiftui icons macos-catalina


【解决方案1】:

使用 AppKit 获取 NSImage 并将其转换为 Image

Image(nsImage: NSWorkspace.shared.icon(forFile: "/Users/stephan/Pictures"))

【讨论】:

  • 感谢您的回答。像魅力一样工作!
猜你喜欢
  • 1970-01-01
  • 2017-08-12
  • 2010-11-05
  • 2017-11-28
  • 2011-10-23
  • 1970-01-01
  • 2021-11-03
  • 2012-09-21
相关资源
最近更新 更多