Sunday, July 2, 2017

Lossless Image Compression (Objective C)

July 02, 2017 Posted by CHANDAN MAKHIJA 1 comment
Hello, welcome again to my tips. Today i will talk about lossless image compression in iOS. You might have come across a situation when you want to upload image to a server. At that time uploading huge size image can take lot of time, so u might have compressed image using UIImageJPEGRepresentation or UIImagePNGRepresentation. So you might wonder that what i will talk on this, as this is a single line of code. But don't worry i will not talk on this. I have something other to tell you today.

What is lossless compression ?

Lossless compression means that as the file size is compressed, the picture quality remains the same, it does not get worse. Also, the file can be decompressed to its original quality.

How it can be done ?

As there are many third party library for this, i have almost used all of them. They either reduce the dimension of the image to reduce its size or does lossy compression. But i have found one third party library that does lossless image compression. I will talk about it in a while. First let me tell you what is WebP format.

What is WebP format ?

WebP is a modern image format that provides superior lossless and lossy compression for images on the web. WebP lossless images are 26% smaller in size compared to PNGs. WebP lossy images are 25-34% smaller than comparable JPEG images at equivalent SSIM quality index.
Lossless WebP supports transparency (also known as alpha channel) at a cost of just 22% additional bytes. For cases when lossy RGB compression is acceptable, lossy WebP also supports transparency, typically providing 3× smaller file sizes compared to PNG.
So you can easily guess what will this library do. Yes, you are right this library converts PNG or JPEG format to WebP format, which will do the lossless image compression to almost 26%. So just click here and download that zip file and then copy UIImage+WebP.h and UIImage+WebP.m into your application. Now import "UIImage+WebP.h". Now use this function to change the image format
+ (void)imageToWebP:(UIImage *)image quality:(CGFloat)quality alpha:(CGFloat)alpha preset:(WebPPreset)preset
    completionBlock:(void (^)(NSData *result))completionBlock
       failureBlock:(void (^)(NSError *error))failureBlock;
Yes now it has been done, if you want to covert it back to the original format then use this function 
+ (void)imageWithWebP:(NSString *)filePath
      completionBlock:(void (^)(UIImage *result))completionBlock
         failureBlock:(void (^)(NSError *error))failureBlock;
I will show you the image which i have compressed, you can compare its quality. Its a lossless compression.

Original Image (1.10 MB)



Compressed Image (856 KB)




--------------------------------------------------------------------------------------------------------------------------

1 comment:

  1. I got here good image optimization tools, thanks, check here my image editing website

    ReplyDelete