Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在UIDocument应用程序中从图像添加缩略图_Ios_Swift_Uidocument - Fatal编程技术网

Ios 在UIDocument应用程序中从图像添加缩略图

Ios 在UIDocument应用程序中从图像添加缩略图,ios,swift,uidocument,Ios,Swift,Uidocument,我一直在尝试为UIDocument应用程序的图标添加缩略图。我举的例子相对简单。通过覆盖UIDocument类中的fileAttributesToWrite函数,将图像分配给thumbnailDictionaryKey,如下所示 注意1:self.thumbnail是我的文档中的图像 注2:我看到了缩略图字典key,在末尾添加和不添加原始值。当我运行它时,我看不出有什么不同 override func fileAttributesToWrite(to url: URL, for saveOper

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

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

注2:我看到了
缩略图字典key
,在末尾添加和不添加原始值。当我运行它时,我看不出有什么不同

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,因为它对可以接收到的内容开放,但对实际处理的内容却很挑剔。我认为您需要为这两个键提供一个
字符串
,并因此使用
rawValue

因此,看看您的示例,缩略图字典也应该有一个

URLThumbnailDictionaryItem.NSThumbnail1024x1024SizeKey.rawValue
另一件您可能不希望依赖于在文件管理器中测试的事情是自己检查url资源,看看是否可以从那里加载图像

像这样的

var-resourceValue:AnyObject?
try(url为NSURL.getPromisedItemResourceValue(&resourceValue,forKey:URLResourceKey.thumbnailDictionaryKey)
让thumbDict=resourceValue为?[字符串:任意]
t=thumbDict?[URLThumbnailDictionaryItem.NSThumbnail1024x1024SizeKey.rawValue]

我也尝试了.rawValue方法。我甚至没有看到调用fileAttributes函数。