使用"复制到应用程序"导入文件时,缩略图元数据消失

Hua*_*ham 13 url metadata thumbnails ios uidocument

我一直在使用URL Resources将缩略图元数据嵌入到我的自定义基于文档的文件中.当我导出自定义文档文件时,在iOS文件应用程序中浏览时,缩略图会很好地显示.

override func fileAttributesToWrite(to url: URL, for saveOperation: UIDocumentSaveOperation) throws -> [AnyHashable : Any] {
    var attr: [AnyHashable : Any] = [URLResourceKey.hasHiddenExtensionKey: true]

    // Ignore the proper resizing for now.
    ...

    attr[URLResourceKey.thumbnailDictionaryKey] = [
        URLThumbnailDictionaryItem.NSThumbnail1024x1024SizeKey: #imageLiteral(resourceName: "Star")
    ]

    return attr
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述 (图标来源:使用你的面包)

但是,当我使用"共享"操作将文件导回到我的应用程序时,除缩略图外Copy to <MyApp>,所有元数据似乎都存在.

在此输入图像描述

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

    // Other non-image metadata survives, like creationDate.
    if let creationDateOpt = try? url.resourceValues(forKeys: [.creationDateKey]).creationDate,
        let creationDate = creationDateOpt {
        print("creationDate: \(creationDate)")
    }


    if let thumbnailDictOpt = try? url.resourceValues(forKeys: [.thumbnailDictionaryKey]).thumbnailDictionary,
        let thumbnailDict = thumbnailDictOpt,
        let thumbnail = thumbnailDict[URLThumbnailDictionaryItem.NSThumbnail1024x1024SizeKey] {
        print ("Thumbnail survived. Size: \(thumbnail.size)")
        return true
    }
    print ("Thumbnail disappeard!")
    return false
}
Run Code Online (Sandbox Code Playgroud)

考虑到当我在"文件"应用程序中手动复制文件时缩略图仍然存在,丢失必须在系统将文件从"文件"复制到应用程序的容器期间发生.为什么会这样?我也可以手动将缩略图作为我文件数据的一部分嵌入,但我觉得应该有办法解决这个问题,因为其他基于文本的元数据仍然存在.

我尝试使用promisedResourceValue方法,怀疑文件在打开时可能没有完全复制,但结果是一样的.

在此输入图像描述

在GitHub上有我的完整项目.