6

While picking image from UIImageController and calculating its size there is variation in image size i.e. size of image in disk is different.

Is there a way to get proper size?

What I have tried -

  1. Converting image to data using UIImagePNGReprensentation & UIImageJPEGReprensentation. The problem with this approach -

a. This approach is memory consuming, so doesn't looks good.

b. Size vary, I can understand that it convert image to data and then calculate size, so size is different.

But whatever I have searched, every accepted answers is around this only.

  1. Using ALAssetsLibrary to get the image size, but size calculated from this also doesn't match the disk size.

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

  1. Using CGImageRef methods CGImageGetBytesPerRow, but this also does't give desired result.

Any other approach which I have missed?

EDIT -

So Here are size difference -

when I check a image size in finder - 5.3 MB

when I check UIImage object size using -

UIImageJPEGRepresentation (image. 1.0) - 2.29 MB

When I check image size using ALAsset Library - 4.4 MB

rishi
  • 11,779
  • 4
  • 40
  • 59
  • Have you considered this factor https://discussions.apple.com/thread/5501534?start=0&tstart=0 – itechnician Jun 23 '16 at 06:21
  • @itechnician I am not moving files between system,what I did is to capture image from iPhone camera, imported it on Mac, checked the image size on mac, but size which i calculate from UIImage varies a lot. – rishi Jun 23 '16 at 06:24
  • what is the actual image size and your calculated size , can you post the variation too – itechnician Jun 23 '16 at 06:47
  • @itechnician - check Edited post above. – rishi Jun 24 '16 at 05:33
  • may i know while you import your image in `MAC` its `PNG` or `JPG` ? as b'cus its matter of file type which affect directly on image size so. – CodeChanger Jun 25 '16 at 13:26
  • @Dhanesh - I have tried with both PNG and JPG, result always varied. Even i have tried identifying the image type and then use UIImagePNGReprensentation or UIImageJPEGReprensentation respectively. – rishi Jun 27 '16 at 05:03
  • The size can vary depending on format. See here: http://stackoverflow.com/questions/1296707/get-size-of-a-uiimage-bytes-length-not-height-and-width – Oren Jun 28 '16 at 22:11

4 Answers4

0

UIImageJPEGRepresentation & UIImagePNGReprensentation is using different compression method and ratio for images so that's why you are getting different sizes of images.

Also ALAssetLibrary uses different image compression method for image that's why you get different sizes of image every time.

Just use any of the three methods to get image size don't try to compare them. Because all three methods use their own image compression methods.

DJ1
  • 936
  • 15
  • 29
0

Like other answers in the post, different image reading techniques will result in varying sizes when comparing on disk file size and in memory file size.

I suggest looking at this SO post. The user is using NSFileSize attribute to read the "actual" size on disk.

I venture to say that there will always be a size difference between what is on disk vs. the size in memory due to the backing mechanism that is used to represent an image in memory vs. an image stored on disk.

You may want to alter your approach. If your goal is to determine the image "size" in memory then you must measure the size against the compression mechanism you use. If your goal is to determine the file "size" when it's on disk then I suggest asking the operating system to give you it's metadata (NSFileSize) about that image on disk. Again, depending on the compression technique you used to save the image to disk, the size comparisons will vary. Using NSFileSize will give you a definitive answer to the "actual" size of the image on disk using a compression technique be that JPEG, PNG, etc.

Community
  • 1
  • 1
TimD
  • 1,090
  • 11
  • 22
-1

I needed something similar and ran into the same issue. Turns out tweaking the quality factor a bit gets this going.

UIImageJPEGRepresentation(image, 0.67)

Ideally the quality should be 1.0 for the image size to be calculated in the most precise form. Turns out it provides you abnormally large values for 1.0 quality.

I couldn't find this documented anywhere and had to keep the above solution in my Photos app. It has been working out okay for my use case. It's not ideal, you might need to further tweak this a bit to best suit your use case.

Tarun Tyagi
  • 9,364
  • 2
  • 17
  • 30
  • check edited post above, have you faced this kind of issue. – rishi Jun 24 '16 at 05:34
  • Not exactly like this. But yes, there were **abnormally large** differences in the file size. In my case, almost all images were JPEG, photos taken by Camera. And I can only confirm this for **UIImageJPEGRepresentation**. – Tarun Tyagi Jun 24 '16 at 05:43
-1

I had an issue where the image size of the captured image was so big I could not manage it so I had to resize it to my 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];
    });
}
Saraswati
  • 1,516
  • 1
  • 14
  • 21
  • have you read the question, my problem is with the size variation and with size variation i specifically mentioned size in MB. Disk size vs size of UIImage Object. – rishi Jun 28 '16 at 08:58