gpt4 book ai didi

ios - 在 UIDocument 应用程序中添加图像缩略图

转载 作者:行者123 更新时间:2023-11-29 05:48:31 29 4
gpt4 key购买 nike

我一直在尝试向 UIDocument 应用程序的图标添加缩略图。我给出的例子相对简单。通过重写 UIDocument 类中的 fileAttributesToWrite 函数,将图像分配给 thumbnailDictionaryKey,如下所示。

注释 1:self.thumbnail 是我文档中的图像。

注释 2:我见过最后添加和不添加 rawValue 的 thumbnailDictionaryKey。当我运行它时,我看不出有什么区别。

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 吗?

最佳答案

如果您没有看到此代码运行,那么问题似乎出在其他地方?您如何打开、保存和关闭文档?

这是一个使用起来很棘手的 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]

关于ios - 在 UIDocument 应用程序中添加图像缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55908749/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com