ios - 如何在 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)
}
解决方案
斯威夫特 4.0
吊舱'SDWebImage'
和'SDWebImage/WebP'
进口SDWebImage
if let image_download = UIImage(data: data) {
let photo:Data = image_download.sd_imageData(as: SDImageFormat.webP)!
}
推荐阅读
- python-3.x - Groupy Pandas 多列数据框以查找上个月的百分比变化
- javascript - 我想在模式中的所有内容上应用 Ajax,但它仅适用于循环中的第一个模式元素
- python - 关于日期操作python pandas的问题
- android - 关于如何让 onResume() 运行 onCreate() 代码的问题
- node.js - 如何使用 nodeJS 在 Aws DynamoDB 中进行更新查询
- android - 软键盘不关闭
- firebase - 在 Flutter 中使用 Firestore 时,使用 Future 而不是 Steam 有什么优势吗?
- node.js - NodeJS - 从哪个节点版本 Object.fromEntries() 可用?
- mysql - MySQL str_to_date 在 Datagrip 中返回日期减去一天
- android - 如何在使用 Retrofit 调用 API 时传递正文中的原始数据