【问题标题】:"unsupported file format 'org.webmproject.webp'" while saving CGImage in webp format“不支持的文件格式‘org.webmproject.webp’”,同时以 webp 格式保存 CGImage
【发布时间】:2021-08-16 11:37:09
【问题描述】:

目前,我们使用PHPickerViewController + CGImage 来考虑有效的内存使用 - https://christianselig.com/2020/09/phpickerviewcontroller-efficiently/

但是,我们在尝试以 webp 格式保存 CGImage 时收到 "unsupported file format 'org.webmproject.webp'" 错误。

func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
    picker.dismiss(animated: true)
    guard !results.isEmpty else { return }

    for result in results {
        result.itemProvider.loadFileRepresentation(forTypeIdentifier: UTType.image.identifier) { (url, error) in
            guard let url = url else { return }
            
            let options: [CFString: Any] = [
                kCGImageSourceCreateThumbnailFromImageAlways: true,
                kCGImageSourceCreateThumbnailWithTransform: true,
                kCGImageSourceShouldCacheImmediately: true,
                kCGImageSourceThumbnailMaxPixelSize: 512
            ]

            guard let imageSource = CGImageSourceCreateWithURL(url as NSURL, nil) else { return }

            let image = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options as CFDictionary)
            
            //
            // No issue in dealing with UTType.jpeg.identifier and UTType.png.identifier.
            // destUrl is type URL.
            //
            guard let destination = CGImageDestinationCreateWithURL(destUrl as CFURL, UTType.webP.identifier, 1, nil) else { return }
            CGImageDestinationAddImage(destination, image!, nil)
            CGImageDestinationFinalize(destination)
        }
    }
}

我们将CGImage 保存为UTType.jpeg.identifier 格式和UTType.png.identifier 格式没有问题。

我可以知道如何将CGImage 保存为 webp 格式而不会出现问题吗?谢谢。

【问题讨论】:

  • 很明显的结论是Core Graphics 不能创建webp 图像——这是一种不受支持的格式。使用 PNG 或 JPEG 或编写您自己的代码将 CGImage 转换为 webp。
  • @Paulw11 但是,如果我们将 webp 图像文件读入 CGImage,并使用 CGImageSourceCreateWithURLCGImageSourceCreateThumbnailAtIndex 和 @ 将其保存为 png/jpeg,知道为什么没有任何问题987654333@?唯一的问题是当将 CGImage 写回 webp 格式时......
  • 错误信息说不支持,所以我猜不支持。也许阅读是但写作不是?

标签: ios swift cgimage photokit


【解决方案1】:

编码不支持webp

您可以使用这样的函数来列出支持的类型:

void listImageCodecs(void){
    CFArrayRef mySourceTypes = CGImageSourceCopyTypeIdentifiers();
    CFShow(mySourceTypes);
    CFArrayRef myDestinationTypes = CGImageDestinationCopyTypeIdentifiers();
    CFShow(myDestinationTypes);
}

在我的系统上我有这样的结果:

(
    "public.jpeg",
    "public.png",
    "com.compuserve.gif",
    "com.canon.tif-raw-image",
    "com.adobe.raw-image",
    "com.dxo.raw-image",
    "com.canon.cr2-raw-image",
    "com.canon.cr3-raw-image",
    "com.leafamerica.raw-image",
    "com.hasselblad.fff-raw-image",
    "com.hasselblad.3fr-raw-image",
    "com.nikon.raw-image",
    "com.nikon.nrw-raw-image",
    "com.pentax.raw-image",
    "com.samsung.raw-image",
    "com.sony.raw-image",
    "com.sony.sr2-raw-image",
    "com.sony.arw-raw-image",
    "com.epson.raw-image",
    "com.kodak.raw-image",
    "public.tiff",
    "public.jpeg-2000",
    "com.apple.atx",
    "org.khronos.astc",
    "org.khronos.ktx",
    "public.avci",
    "public.heic",
    "public.heics",
    "public.heif",
    "com.canon.crw-raw-image",
    "com.fuji.raw-image",
    "com.panasonic.raw-image",
    "com.panasonic.rw2-raw-image",
    "com.leica.raw-image",
    "com.leica.rwl-raw-image",
    "com.konicaminolta.raw-image",
    "com.olympus.sr-raw-image",
    "com.olympus.or-raw-image",
    "com.olympus.raw-image",
    "com.phaseone.raw-image",
    "com.microsoft.ico",
    "com.microsoft.bmp",
    "com.apple.icns",
    "com.adobe.photoshop-image",
    "com.microsoft.cur",
    "com.truevision.tga-image",
    "com.ilm.openexr-image",
    "org.webmproject.webp",
    "com.sgi.sgi-image",
    "public.radiance",
    "public.pbm",
    "public.mpo-image",
    "public.pvr",
    "com.microsoft.dds",
    "com.apple.pict"
)
(
    "public.jpeg",
    "public.png",
    "com.compuserve.gif",
    "public.tiff",
    "public.jpeg-2000",
    "com.apple.atx",
    "org.khronos.ktx",
    "org.khronos.astc",
    "com.microsoft.dds",
    "public.heic",
    "public.heics",
    "com.microsoft.ico",
    "com.microsoft.bmp",
    "com.apple.icns",
    "com.adobe.photoshop-image",
    "com.adobe.pdf",
    "com.truevision.tga-image",
    "com.ilm.openexr-image",
    "public.pbm",
    "public.pvr"
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    • 2019-05-25
    • 2010-11-22
    • 2013-11-25
    相关资源
    最近更新 更多