【问题标题】:MacOS and Swift 4: reading URLResourceKey.customIconKey and URLResourceKey.thumbnailKey file attributesMacOS 和 Swift 4:读取 URLResourceKey.customIconKey 和 URLResourceKey.thumbnailKey 文件属性
【发布时间】:2019-04-03 14:24:20
【问题描述】:

我正在尝试使用 Swift 4 应用程序读取 URLResourceKey.customIconKeyURLResourceKey.thumbnailKey 文件属性;你猜我有一些问题,因为不会返回NSImage

问题:以上两个键是指自定义图标(即Adobe Acrobat文档图标)和png/jpeg文件的缩略图吗?

如果不是,那两个键到底指的是什么?

Apple 文档的描述非常简短,谷歌没有给出任何结果。

顺便说一下,这是我正在处理的代码。

    // Get file informations
    let requiredAttributes = [URLResourceKey.nameKey, URLResourceKey.creationDateKey, URLResourceKey.contentModificationDateKey, URLResourceKey.fileSizeKey, URLResourceKey.isPackageKey, URLResourceKey.thumbnailKey, URLResourceKey.customIconKey]
    do {
        let properties = try (localURL as NSURL).resourceValues(forKeys: requiredAttributes)
        ...
        let customIcon = properties[URLResourceKey.customIconKey] as? NSImage ?? nil
        if customIcon != nil {
            let saveURL = URL(fileURLWithPath: "/Users/foo/Desktop/fileicon.png")
            try customIcon!.savePNGRepresentationToURL(url: saveURL)
        }

        let customThumbnail = properties[URLResourceKey.thumbnailKey] as? NSImage ?? nil
        if customThumbnail != nil {
            let saveURL = URL(fileURLWithPath: "/Users/foo/Desktop/filethumbnail.png")
            try customThumbnail!.savePNGRepresentationToURL(url: saveURL)
        }
   } catch {...}

检查properties 字典时,customIconKeythumbnailKey 两个没有设置。

感谢您的帮助。

【问题讨论】:

  • 我认为你应该做一组像let requiredAttributes: Set<URLResourceKey> = ...这样的键,使用URL而不是NSURL,然后let properties = try localURL!.resourceValues(forKeys: requiredAttributes),然后访问像properties.thumbnail这样的成员。
  • @ayaio - 你能给我指出你提出的解决方案的工作样本吗?
  • @ayaio - 我尝试了你的建议,但没有任何改变......
  • 嗯,可惜好像没有实现?! screenshot 这很奇怪,我可以发誓我几年前就用过它,而且效果很好……我不明白。
  • 我正在莫哈韦进行测试。你也是?也许它适用于以前的 macOS 版本。我不知道为什么它不再可用了。

标签: swift macos


【解决方案1】:

通过将“URLResourceKey.customIconKey”替换为“URLResourceKey.effectiveIconKey”,让“properties”打印以下消息:

NSURLEffectiveIconKey): <NSImage 0x60c000075fc0 Size={128, 128} Reps=(
    "<NSIconRefImageRep:0x60c00008af50 iconRef=0x103 size:128x128 pixels:128x128>",
    "<NSIconRefImageRep:0x60c00008aff0 iconRef=0x103 size:128x128 pixels:256x256>",
    "<NSIconRefImageRep:0x60c00008b040 iconRef=0x103 size:256x256 pixels:256x256>",...

有帮助吗?

【讨论】:

    猜你喜欢
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    相关资源
    最近更新 更多