首页 > 解决方案 > 如何在 Swift 中将 PNG 转换为 WebP?

问题描述

我正在尝试在 Swift 中转换 Webp 中的 PNG 图像,我工作的唯一框架是OpenCV通过Objective-c++
问题是,如果我将图像大小调整为 512x512(这是我需要的分辨率),它会崩溃: 在此处输入图像描述 如果我将图像(使用 OpenCV 或使用 Swift)调整为另一个分辨率(例如 510x510),它不会崩溃。

奇怪的是,在模拟器上它从不崩溃,而在 iPhone XS 上它崩溃了 90% 的时间。

如何在 Swift 中将 PNG 转换为 Webp?如果 Mat 是 512x512,
为什么 OpenCV 会在imwrite指令上崩溃?

更新
OpenCV 版本:3.4.2
我发现这个问题发生在之前从 Core Graphics 框架处理 PNG 图像时。我需要使用它,因为我以这种方式将 UIVIew 保存为 UIImage:

let renderer = UIGraphicsImageRenderer(bounds: bounds)
        return renderer.image { rendererContext in
            layer.render(in: rendererContext.cgContext)
        }

标签: iosswiftopencvobjective-c++

解决方案


斯威夫特 4.0

吊舱'SDWebImage''SDWebImage/WebP'

进口SDWebImage

if let image_download = UIImage(data: data) {
   let photo:Data = image_download.sd_imageData(as: SDImageFormat.webP)!
}

推荐阅读