I have a Uiimage (usually with transparency) and I need to make it 512x512 and at most 100kb. I was able to resize to 512x512 (but the images were around 400kb), however and when I use the jpegData (compressionQuality: 0.3) method I lose the transparency of the image.
let imagePNG = image.jpegData(compressionQuality: 0.3)
try stickerPack.addSticker(imageData: imagePNG!, type: ImageDataExtension(rawValue: "png")!, emojis: [""])
Is there any way to reduce the size of the image without losing transparency?
(I have this problem before when creating WhatsApp Sticker Packs)
If it is possible, you can try Google's WebP format. It is a file format to minimize the image size and not to lose the transparency. You can try the libwebp official library to compress and encode your image, but I recommend you to use YYImage. After installing YYImage, use YYCGImageCreateEncodedWebPData()
to create and compress your CGImage (UIImage.cgImage?
) to WebP data (more accurately, Unmanged<CFData>
).
Hope this help.