【问题标题】:QLThumbnailGenerator saveBestRepresentation wrong content type Swift iOS 13+QLThumbnailGenerator saveBestRepresentation 错误的内容类型 Swift iOS 13+
【发布时间】: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 支持的类型,例如kUTTypePNGkUTTypeJPEG.'。不幸的是,kUTTypePNGkUTTypeJPEG 都已弃用。在这种情况下应该使用哪种内容类型?

【问题讨论】:

标签: ios swift xcode foundation quicklook


【解决方案1】:

iOS 14 方式:

在文件的顶部,import UniformTypeIdentifiers。现在改变

contentType: "image/jpg"

contentType: UTType.jpeg.identifier

【讨论】:

    【解决方案2】:

    “public.png”对我有用。

    【讨论】:

      【解决方案3】:

      MIME 类型(例如“image/jpg”)和 UTI(例如“public.jpeg”)是两个不同的东西。 Apple API 通常采用 UTI,包括您尝试使用的 UTI。像 kUTTypeJPEG 这样的符号只是常量字符串的名称。如果您愿意,可以使用字符串文字代替符号名称。

      一些常见的例子:

      "public.jpeg"  (kUTTypeJPEG)
      "public.png"  (kUTTypePNG)
      "com.compuserve.gif" (kUTTypeGIF)
      "public.tiff"  (kUTTypeTIFF)
      "public.heic"  (no symbolic name defined)
      

      为了好玩,“com.microsoft.bmp”(kUTTypeBMP)怎么样

      如果你知道 MIME 类型,你可以像这样得到 UTI:

      uti = UTTypeCreatePreferredIdentifierForTag( kUTTagClassMIMEType, mimeType, (CFStringRef)0);
      

      如果你知道 UTI,你可以像这样得到 MIME 类型:

      mimeType = UTTypeCopyPreferredTagWithClass( uti, kUTTagClassMIMEType);
      

      如果您想要在您的案例中使用的 UTI 列表,您可以:

      CFArrayRef utis = CGImageDestinationCopyTypeIdentifiers();
      

      (这些都是C sn-ps。Swift可能略有不同。)

      【讨论】:

      • 否,在 iOS 14 中,不推荐使用 kUTTypePNG 和 kUTTypeJPEG 以及类似的常量。
      • @matt 我认为如果您建议我不能再将 UTI 传递给采用 UTI 的 API,您应该再次检查。我的建议是使用字符串文字,而不是声称已弃用的符号名称。
      • 我是说对于 iOS 14,这不是弃用问题的正确答案。您肯定正确地指出 mime 类型不是 UTI!但是到达有问题的 UTI 的正确方法是例如UTType.jpeg.identifier.
      • @matt,那么正确的答案到底是什么?我正在使用这个确切的 API。 Apple 从来没有为所有的 UTI 命名——从来没有。使用字符串而不是这些常量总是合适的,因为 Apple 进行字符串比较而不是指针比较。只需调用 CGImageDestinationCopyTypeIdentifiers 即可查看 ImageIO 中越来越多的 UTI。我的机器上现在没有 iOS 14 SDK,所以我不知道有多少使用 UTI 的 API 被标记。那么如果不是字符串,你应该传递什么作为 UTI 参数?
      • @matt,抱歉,通过调用 CGImageSourceCopyTypeIdentifiers 可以看到不断增长的 UTI 列表的更好示例。
      猜你喜欢
      • 2015-12-28
      • 2021-05-20
      • 1970-01-01
      • 2021-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-12
      相关资源
      最近更新 更多