'Why image size get increased after writing in Document directory?

        let str = localpath?.path
        let image = UIImage(contentsOfFile: str!)

Original Size = 228 kb Size after writing file = 700 kb

Now, My question is that, why writing file into document directory 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?.

After Research I found this reason. Reason - 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/

I watched the full WWDC video, and I got the reason behind this problem. But what I just want is, get image from imagepickercontroller, show it to user, once user confirms, then upload to my server(via multipart). Now, if I am uploading this image(rendered one), I am getting this larger size on server too. Is there any way, by which I may have rendered image different and uploaded image different(with actual filesize). Even if I am saving the image after rendering, my file size is still same as rendered one.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source