Search code examples
iosuiimageuiimagepickercontrolleruiimagejpegrepresentationuiimagepngrepresentation

Why image size get increased after UIImagePNGRepresentation?


I have image file in my photos app of iphone.

I have taken it to my macbook, via airdrop and checked size of the image = 1.9 MB

I saved the same image to Files app of iphone and checked the image size there = 1.9 MB

I have taken same image into my app viaUIImagePickerController,

  • Used UIImagePNGRepresentation and printed the data count/byte count = 15.8 MB
  • Used UIImageJPEGRepresentation with compressionRatio 1.0 and printed the data count/byte count = 5.4 MB
  • Used UIImagePNGRepresentation and saved it to local(DocumentsDirectory) and checked the file size = 15.8 MB
  • Used UIImageJPEGRepresentation with compressionRatio 1.0 and saved it to local(DocumentsDirectory) and checked the file size = 5.4 MB

Now, My question is that, why UIImagePNGRepresentation OR UIImageJPEGRepresentation increases file size to that much large?. I even uploaded this image to my server and obviously getting this larger image sizes. I wanted to upload given image to my server with the original quality, but i dont want this much large sized image(as its actual size on device is totally different), is there any other way by which we can prevent original image size and quality both?.


Solution

  • Let's take this example from WWDC 2018 session - 416_ios_memory_deep_dive. If you have 590kb file size image, the dimension of image is 2048 pixels x 1536 pixels. SO the total memory of this image is 10MB (2048 pixels x 1536 pixels x 4 bytes per pixel) For more details you can take a look at this video. https://developer.apple.com/videos/play/wwdc2018/416/