简体   繁体   中英

UIImage size variation when picked from UIImagePickerController

While picking image from UIImageController and calculating its size there is variation in image size ie 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

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.

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.

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.

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];
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM