我一直在尝试为UIDocument
应用程序的图标添加缩略图。我所举的例子是相对简单的。通过覆盖UIDocument类中的thumbnailDictionaryKey
函数,将图像分配给fileAttributesToWrite
,如下所示。
注释1 :self.thumbnail是我文档中的图像。
注释2 :我已经看到thumbnailDictionaryKey
的末尾添加和未添加rawValue。运行它时,我看不出有什么区别。
override func fileAttributesToWrite(to url: URL, for saveOperation: UIDocument.SaveOperation) throws -> [AnyHashable : Any] {
var attributes = try super.fileAttributesToWrite(to: url, for: saveOperation)
print("in fileAttributes")
if let thumbnail = self.thumbnail {
attributes[URLResourceKey.thumbnailDictionaryKey.rawValue] =
[URLThumbnailDictionaryItem.NSThumbnail1024x1024SizeKey:thumbnail]
}
return attributes
}
代码正在编译,但未按要求在缩略图上显示图像。在iOS文件管理器中,它仍然是应用程序的图标。
我要注意的一件事是我没有看到fileAttributesToWrite
函数正在执行。 (打印行在那里进行检查。)
我要离开的地方有步骤吗?我需要强制fileAttributesToWrite
运行吗?
答案 0 :(得分:0)
如果您没有看到这段代码在运行,听起来问题出在其他地方吗?您如何打开,保存和关闭文档?
使用它是一个棘手的API,因为它对可以接收的内容开放,但对其实际处理的内容有些挑剔。我相信您需要为这两个键都提供一个String
,因此请使用rawValue
。
所以在您的示例中,缩略图字典也应该有一个
URLThumbnailDictionaryItem.NSThumbnail1024x1024SizeKey.rawValue
您可能不想尝试在文件管理器中进行测试的另一件事是自己检查url资源,以查看是否可以从那里加载图像。
类似这样的东西
var resourceValue: AnyObject?
try (url as NSURL).getPromisedItemResourceValue(&resourceValue, forKey: URLResourceKey.thumbnailDictionaryKey)
let thumbDict = resourceValue as? [String: Any]
t = thumbDict?[URLThumbnailDictionaryItem.NSThumbnail1024x1024SizeKey.rawValue]