iOS: Jpeg compression increasing the Size of image (Swift) - ios

I'm trying to attach a photo(UIImage) in my iOS application from the Photos app. The original size of the image is ~10MB but when I process it with
image?.jpegData(compressionQuality: 0.7)
the output size of the image becomes ~25MB.
Can anyone suggest as to what might be happening here?

Related

Find image size and optimise in Swift

In my application, I'm using YPImagepicker for selecting images from library and camera. I want to know image size in MB after selecting the pictures or capturing a photo. Task is to convert the images into data and send to backend via REST API. As of now we are limiting images into 5. So I want to see the size of every images if it is more than 1 Mb need to compress into 1mb.
let imgData = NSData(data: image.jpegData(compressionQuality: 1)!)
var imageSize: Int = imgData.count
print("actual size of image in KB: %f ", Double(imageSize) / 1024.0 / 1024.0)
the above sample I have used to check the size of the image but I'm not seeing the correct file size. For eg, I'm capturing one photo through app and it is getting saved in album when I check the image size it shows 3.4 MB in photo detail but in code I'm not getting the same size. What is best way to achieve this?
Apple doesn’t use JPEG for storing images in your iOS library. They use a proprietary file format with its own lossy compression.
The two file formats will yield different file sizes.
When you load an image from the user’s image library and convert it to JPEG data, it gets re-compressed using JPEG compression with the image quality value you specify. A compressionQuality value of 1.0 will create an image file with the best image quality but the largest file size. That is why your results are bigger than the files from the user’s image library. Try a lower compressionQuality value.

Size of document increases on iOS device compare to the Android/Windows device

There is one very interesting issue I face at the moment in my iOS application.
The image size got increased by the random number of percentage.
What I have observed is as below
When I choose the same image from the photo library and try to send the image by converting it to data, though the multipart form data API.
The image size also got increased in multiple times of the original image size.
I use the below code to convert the image into data bytes
img.jpegData(compressionQuality: 1.0)
The data length is around 90 MB.
The original image is available here.
Does anyone know where is the issue and how to resolve it?

Image optimisation using the Kingfisher library in iOS when loading an image?

I'm loading images on a list from a server. The UIImageView size is very small (40 x 40), So ideally an image with a size of 40 x 40 or a little larger should be loaded in it. However, the server guy is sending an image of a much larger size (1920 x 1200).
When I diagnosed the view controller memory I noticed that it increases as the image is loading.
So my question is
Will the Kingfisher library download a smaller size version of the image?
Will it abort the image loading if the image size is too big?
Is there any way by which can I specifically achieve the above task.
Kingfisher: https://github.com/onevcat/Kingfisher
Will the Kingfisher library download a smaller size version of the
image?
Nor Kingfisher nor any other library will download a smaller version of an image. The server has to send you a smaller version of it. (different url, or same url with a parameter)
Will it abort the image loading if the image size is too big?
I don't know how the library implements the downloading but unless you are talking about unreasonably big files (way bigger than the resolution you expect (1920x1200)) it will not be aborted. And even then, it probably won't be aborted but instead the app might be killed by the system when displaying super high resolution images (again, way bigger than 1920 x 1200)
Is there any way by which can I specifically achieve the above task.
If you want to display many many images at once and are worried about their sizes, its better to create thumbnails of them.
You can use Kingfisher to
1) first download the image
2) then resize the image
3) then show and cache it if you need
Check this page to see a ton of code snippets for that library.
https://github.com/onevcat/Kingfisher/wiki/Cheat-Sheet

UIImageJPEGRepresentation crashes in Share Extension on iPhone SE

I have a share extension that lets you crop an image and then upload it to our service. We call UIImageJPEGRepresentation to get the image's data before we upload it, but causes a crash due to memory excessive memory. This only happens with large images, and (as far as we can tell) on the SE, and didReceiveMemoryWarning is not called first. This is happening when using the Photos app.
Is there anyway to safely call UIImageJPEGRepresentation, or try to determine if the image is too large beforehand?
Why not check for the image file size? If the image file size exceeds a certain quota, then resize it.
let image: Data = UIImagePNGRepresentation(image)
var imageSize: Double = (image.length)/1024 // in KB

How to show Blurred image before downloading(like in whatsapp) in ios?

I am making a Chat application for image sending and downloading. I want to do some animation like in WhatsApp. So, how to show the blurred image? My image is downloaded from server. Please help me.
Scale the image down to a very small size like say 8x8 pixels. Send that data to the client. It should not be more than 4 * 8 * 8 = 256 B or 0.25 KB in size, so sending it to the client will not be a problem no matter how slow the client's connection speed is.
Then scale the received 8x8 image to the resolution of the preview box (e.g. 256x256). You shouldn't need to upsample the 8x8 image if the preview box is a UIImageView (see the link below). This downsampling and successive upsampling of an image "blurs" the image.
To understand how you can resample/rescale an image in iOS, check this link. I am not an iOS developer and cannot help you much in performing this task specifically for iOS.
I think that process is done using the server... As I know, there are 2 copies for a single image..
one image with full quality
other image is a thumbnail of original image.. (low size in capacity )
So.. before downloading the original picture client app calls for the URL of the thumbnail & it downloads very quickly since its size is less..
Meanwhile you can use an async function to download original file in the same time..
That is the process...
Hope it will be help for you!!

Resources