Why image size get increased after UIImagePNGRepresentation? - ios

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?.

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/

Related

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.

Large jpeg using more memory than smaller jpeg when loaded in a UIImageView. Why?

The login view in our app uses large background images. To try and save on memory/app size I resized and compressed these images, which reduced their filesize significantly (less than 1mb, down from several mb).
When monitoring my apps memory usage (XCode debugger) there is a clear spike when a modified image is displayed (around 30-40mb). I'd accepted this as normal and simply made sure to release the image asap to limit memory usage.
I've recently started replacing a couple of the images and wanted to preview the new ones before resizing/compressing them. I noticed that these images (one of which is 11mb on disk and 4640x3472 pixels) has no visible effect on app memory usage whatsoever, increasing 'Other Processes' instead (by around 20-30mb).
Can anyone explain what's happening here? I want to confirm it is advisable to continue resizing/compressing the images.
Note that I'm loading the images using UIImage(contentsOfFile:) and I resized/compressed the images using GIMP. The new images have been taken straight from Flickr and unmodified.
Cheers.
The in-memory size of the image (as a UIImage) is different to the compressed on-disk size (your JPEG)
The UIImage takes 4 bytes (RGBA) per pixel x height x with - so for a 4640 x 3472 image, you're looking at 64,440,320 bytes - quite different to the 11MB on disk

Downgrade UIImage quality to max bytes

I have an application where I require a given UIImage to be 300x300px in physical size and less than 500kb in file size.
The image is taken from the device's photo library and resized to 300x300px.
In most cases the resulting photo is less than 500kb, but depending on the original image (such as resolution) it may be larger.
I do not want to downsize the image further, as I require it to be exactly 300px in its largest dimension.
How can I downgrade the quality on those images to a point where they are less than 500kb in file size (PNG representation)?

iOS not respecting jpeg image 32MP limit - how to fix

As title states and as searching on google can give, on iOS there is a limit for what the devices can handle for jpeg images.
As per Apple docs (Know iOS Resource Limits):
Because of the memory available on iOS, there are limits on the number
of resources it can process:
The maximum size for decoded GIF, PNG, and TIFF images is 3 megapixels
for devices with less than 256 MB RAM and 5 megapixels for devices
with greater or equal than 256 MB RAM. That is, ensure that width *
height ≤ 3 * 1024 * 1024 for devices with less than 256 MB RAM. Note
that the decoded size is far larger than the encoded size of an image.
The maximum decoded image size for JPEG is 32MP megapixels using
subsampling. JPEG images can be up to 32 megapixels due to
subsampling, which allows JPEG images to decode to a size that has one
sixteenth the number of pixels. JPEG images larger than 2 megapixels
are subsampled—that is, decoded to a reduced size. JPEG subsampling
allows the user to view images from the latest digital cameras.
I added the enfasis on the point that's bugging me mostly. I'm trying to display a fairly big image, but still largely in the above 32MP mentioned limit, specifically its a 3995px * 2138px for a total of 8.5MP and 396kb weight (jpeg quality/compression set to 25 via PS).
Still whenever I call for that image as ex. source of an <img> tag, nothing is displayed on any iOS device I've been able to test, on emulators and couple real devices (iphone4, ipad2, 3, mini...).
Is there anything I'am missing blatantly or maybe I've not understand from the docs above?
What can I do apart replace it with a reduced file size? If forced to replace it, what is the highest width I can reach without breaking? How can I ensure iOS honor the 32MP limit mentioned?
I'm speaking in a website perspective, not a native app on the device.
It doesn't fix your current problem but if you look at image handling in IOS8 there are no longer any image size limits (CoreImage can automatically tile) - perhaps you could target that?
You can split up images and tile them.I routinely display images 180,000 x 120,000 pixels on IOS devices by chopping them up and using a CATiledLayer.

What is the difference in size between a decoded and encoded image

In the Safari Web Content Guide there is a section entitled "Known iOS Resource Limits." In this section they warn:
The maximum size for decoded GIF, PNG, and TIFF images is 3 megapixels for devices with less than 256 MB RAM and 5 megapixels for devices with greater or equal than 256 MB RAM.
Then go on to note that there is large different in decoded vs encoded image sizes
Note that the decoded size is far larger than the encoded size of an image.
So assume I have a .png image on my hard drive that says it's 2794 × 1859 = ~ 5,400,000 pixels (just under 5 megapixel the limit) and a file size of 5.4 MB. Is this the encoded size of the image or the decoded size of the image? And how do I figure out what the other size is?

Resources