Resize UIImage when choosing from UIImagePickerController

When you select an image from the UIImageController and calculate its size, the image resizes, that is, the image size on the disk is different.

Is there any way to get the right size?

What I tried -

  • Convert an image to data using UIImagePNGReprensentation and UIImageJPEGReprensentation . The problem with this approach is

a. This approach is memory consumed, so it looks good.

b. The size varies, I can understand that it converts the image into data and then calculates the size, so the size is different.

But no matter what I was looking for, all the accepted answers are just around this.

  1. Using ALAssetsLibrary to get the image size, but the size calculated from this also does not match the disk size.

I used the following method - assetForURL: resultBlock:^(ALAsset *asset) failureBlock:

  1. Using the CGImageRef methods CGImageRef , but this also does not give the desired result.

Any other approach I missed?

EDIT -

So here is the size difference -

when I check the image size in finder - 5.3 MB

when I check the size of a UIImage object with -

UIImageJPEGRepresentation (Image 1.0) - 2.29 MB

When I check image size using ALAsset library - 4.4 MB

+5
source share
4 answers

UIImageJPEGRepresentation and UIImagePNGReprensentation uses a different compression method and ratio for images, so you get different image sizes.

ALAssetLibrary also uses a different image compression method for the image, so each time you get a different image size.

Just use any of the three methods to get the image size, do not try to compare them. Since all three methods use their own image compression methods.

0
source

Like other answers to this topic, different image reading methods will have different sizes when compared with the disk file size and the memory file size.

I suggest taking a look at this SO post. The user uses the NSFileSize attribute to read the "actual" size on the disk.

I dare say that there will always be a difference in size between what is on the disk and the size in memory due to the backup mechanism that is used to represent the image in memory compared to the image stored on disk.

You can change your approach. If your goal is to determine the size of the image in memory, then you should measure the size using the compression mechanism used. If your goal is to determine the size of the file when it is on disk, I suggest asking the operating system to provide you with metadata (NSFileSize) about that image on disk. Again, depending on the compression technique you used to save the image to disk, the size comparison will vary. Using NSFileSize will give you the final answer to the "actual" size of the image on disk using compression techniques: JPEG, PNG, etc.

0
source

I need something similar and ran into the same problem. It turned out that the quality factor setting improved slightly.

 UIImageJPEGRepresentation(image, 0.67) 

Ideally, quality should be 1.0 in order for the image size to be calculated in the most accurate form. It turns out that this gives you abnormally large values โ€‹โ€‹for 1.0 quality.

I could not find this documentation anywhere and had to save the above solution in the application โ€œMy photosโ€. It works well for my use case. This is not ideal, you may need to adjust it a bit to best suit your use case.

-1
source

I had a problem when the image size of the captured image was so large that I could not control it, so I had to resize it to the desired size ... I am adding my code here

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { dispatch_async(dispatch_get_main_queue(), ^{ for(UIView *v in imgView.subviews) { [v removeFromSuperview]; } CGSize newSize=CGSizeMake(200, 200); UIImage *image = [info valueForKey: UIImagePickerControllerOriginalImage]; UIGraphicsBeginImageContext(newSize); [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); [imgView setImage:newImage]; [self.view layoutIfNeeded]; [self.navigationController dismissViewControllerAnimated: YES completion: nil]; }); } 
-1
source

All Articles