【发布时间】: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,并使用
CGImageSourceCreateWithURL、CGImageSourceCreateThumbnailAtIndex和 @ 将其保存为 png/jpeg,知道为什么没有任何问题987654333@?唯一的问题是当将 CGImage 写回 webp 格式时...... -
错误信息说不支持,所以我猜不支持。也许阅读是但写作不是?
标签: ios swift cgimage photokit