I'm trying to convert a PNG image in Webp in Swift, the only framework I got working is OpenCV through Objective-c++.
The problem is that if I resize the image by 512x512 (that's the resolution I need) it crashes:
If I resize the image (either with OpenCV either with Swift) to another resolution (ex 510x510) it doesn't crash.
The strange thing is that on the simulator it never crashes while on the iPhone XS it crashes 90% of the times.
How can I convert a PNG to a Webp in Swift?
Why is OpenCV crashing on the imwrite instruction if the Mat is 512x512?
UPDATE:
OpenCV version: 3.4.2
I found out that this problem happens when the PNG image get processed before from the Core Graphics framework. I need to use it since I save a UIVIew as UIImage this way:
let renderer = UIGraphicsImageRenderer(bounds: bounds)
return renderer.image { rendererContext in
layer.render(in: rendererContext.cgContext)
}
I ended up using another framework to convert PNG to WebP: https://github.com/seanooi/iOS-WebP, had to create the wrapper to use it on swift but it works very good 😊
My wrapper is very simple but does what I needed:
#import <Foundation/Foundation.h>
#import "WebPWrapper.h"
#import "UIImage+WebP.h"
@implementation WebPWrapper
-(NSData *)convertUIImageToWebp:(UIImage *)image :(int)quality {
NSData *webPData = [UIImage imageToWebP:image quality:quality];
return webPData;
}
@end
In swift I use it this way:
let webPData = WebPWrapper().convertUIImage(toWebp: image, 90)