【发布时间】:2020-07-28 10:23:12
【问题描述】:
在完成一项任务时,我偶然发现了一个可以解决我的问题的 iOS 13+ API - https://developer.apple.com/documentation/quicklookthumbnailing/creating_quick_look_thumbnails_to_preview_files_in_your_app
它按预期工作,但要完成我的任务,我需要将创建的缩略图保存到磁盘,我正在尝试使用 - https://developer.apple.com/documentation/quicklookthumbnailing/qlthumbnailgenerator/3237298-savebestrepresentation
func saveBestRepresentation(for request: QLThumbnailGenerator.Request,
to fileURL: URL,
contentType: String,
completion completionHandler: @escaping (Error?) -> Void)
这是我的代码:
/// Uses quick look to asynchronously create best possible thumbnail image at path
static func createThumbnailFor(_ inputUrl: URL, at outputUrl: URL) {
let size: CGSize = CGSize(width: 60, height: 90)
let scale = UIScreen.main.scale
// Create the thumbnail request.
let request =
QLThumbnailGenerator.Request(
fileAt: inputUrl,
size: size,
scale: scale,
representationTypes: .all)
// Retrieve the singleton instance of the thumbnail generator and generate the thumbnails.
let generator = QLThumbnailGenerator.shared
generator.saveBestRepresentation(for: request, to: outputUrl, contentType: "image/jpg") { (error) in
if let error = error {
print(error.localizedDescription)
}
}
}
我收到以下错误:
2020-07-28 13:08:01.259835+0300 SomeAppName[4748:1236635] *** Terminating app due to uncaught exception 'QLThumbnailGeneratorInvalidContentType', reason: 'image/jpg is not a supported image type'
我尝试了许多不同的类型并查看了标题,但它只显示“您要保存的缩略图的内容类型。使用CGImageDestination 支持的类型,例如kUTTypePNG 或kUTTypeJPEG.'。不幸的是,kUTTypePNG 和 kUTTypeJPEG 都已弃用。在这种情况下应该使用哪种内容类型?
【问题讨论】:
-
你试过“image.png”吗? developer.apple.com/documentation/uniformtypeidentifiers/…
-
iOS 14 尚未发布。它看起来确实像一个系统错误。很可能,他们会添加另一个带有 [UTType] 参数的 API。
标签: ios swift xcode foundation quicklook