【发布时间】:2019-04-03 14:24:20
【问题描述】:
我正在尝试使用 Swift 4 应用程序读取 URLResourceKey.customIconKey 和 URLResourceKey.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 字典时,customIconKey 和thumbnailKey 两个没有设置。
感谢您的帮助。
【问题讨论】:
-
我认为你应该做一组像
let requiredAttributes: Set<URLResourceKey> = ...这样的键,使用URL而不是NSURL,然后let properties = try localURL!.resourceValues(forKeys: requiredAttributes),然后访问像properties.thumbnail这样的成员。 -
@ayaio - 你能给我指出你提出的解决方案的工作样本吗?
-
@ayaio - 我尝试了你的建议,但没有任何改变......
-
嗯,可惜好像没有实现?! screenshot 这很奇怪,我可以发誓我几年前就用过它,而且效果很好……我不明白。
-
我正在莫哈韦进行测试。你也是?也许它适用于以前的 macOS 版本。我不知道为什么它不再可用了。