Thumbnail metadata disappeared when importing a copy file to an application

I use URL Resources to embed thumbnail metadata in my document-based custom file. When I export my own document file, the thumbnail displays well when viewed in the iOS Files application.

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 } 

enter image description here (Icon Credit: Use Your Bread )

However, when I import the file back into my application with the Share, Copy to <MyApp> action, all the metadata seems to survive , except for the thumbnail .

enter image description here

 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 } 

Given that the thumbnail is saved when I manually copy the file in the Files application, the loss should occur while copying the file from the file to my application container. Why is this happening? I can manually insert a thumbnail as part of my files, but I believe that there should be a way to solve this problem, as other text metadata is saved.

I tried using the promisedResourceValue method instead, suspecting that the file could not be fully copied when it was opened, but the result is the same.

enter image description here

I have my complete project here on GitHub .

+5
source share

Source: https://habr.com/ru/post/1274653/


All Articles