Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Size of UIImage in Bytes in iOS 4.0+?

I am trying to pick an image from the photo library or from the camera.
The delegate method:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo

Gives me the UIImage object. I need to find the size of the image in bytes for my application.

Is there any way I can get the file type of the image and also the size in the bytes?

Any kind of help would be highly appreciated.

Thanks in advance

like image 994
devsri Avatar asked Jun 17 '11 11:06

devsri


People also ask

How do I find my UIImage path?

Once a UIImage is created, the image data is loaded into memory and no longer connected to the file on disk. As such, the file can be deleted or modified without consequence to the UIImage and there is no way of getting the source path from a UIImage.

What is the difference between a UIImage and a UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage . Save this answer.

How do you create a UIImage?

To create a new UIImage programmatically in Swift, we simply need to create a new instance of UIImage providing it with the name of the image we have added to a Resources folder. Once we have an instance of UIImage created, we can add it to a UIImageView.


3 Answers

Try the following code:

NSData *imageData = [[NSData alloc] initWithData:UIImageJPEGRepresentation((image), 1.0)];

int imageSize = imageData.length;
NSLog(@"SIZE OF IMAGE: %i ", imageSize);
like image 165
Asish AP Avatar answered Oct 21 '22 07:10

Asish AP


I know this is an old question but creating a NSData object just to get the byte-size of an image can be a really expensive operation. Image can have over 20Mb and creating equally sized object just to get the size of the first one...

I tend to use this category:

UIImage+CalculatedSize.h

#import <UIKit/UIKit.h>

@interface UIImage (CalculatedSize)

-(NSUInteger)calculatedSize;

@end

UIImage+CalculatedSize.m

#import "UIImage+CalculatedSize.h"

@implementation UIImage (CalculatedSize)

-(NSUInteger)calculatedSize
{    
    return CGImageGetHeight(self.CGImage) * CGImageGetBytesPerRow(self.CGImage);
}

@end

You simply import the UIImage+CalculatedSize.h and use it like this:

NSLog (@"myImage size is: %u",myImage.calculatedSize);

Or, if you want to avoid using categories:

NSUInteger imgSize  = CGImageGetHeight(anImage.CGImage) * CGImageGetBytesPerRow(anImage.CGImage);

EDIT:

This calculation of course has nothing to do with JPEG/PNG compression. It relates to underlaying CGimage:

A bitmap (or sampled) image is a rectangular array of pixels, with each pixel representing a single sample or data point in a source image.

In a way a size retrieved this way gives you a worst-case scenario information without actually creating an expensive additional object.

like image 37
Rok Jarc Avatar answered Oct 21 '22 06:10

Rok Jarc


From:@fbrereto's answer:

The underlying data of a UIImage can vary, so for the same "image" one can have varying sizes of data. One thing you can do is use UIImagePNGRepresentation or UIImageJPEGRepresentation to get the equivalent NSData constructs for either, then check the size of that.

From:@Meet's answer:

 UIImage *img = [UIImage imageNamed:@"sample.png"];
 NSData *imgData = UIImageJPEGRepresentation(img, 1.0); 
 NSLog(@"Size of Image(bytes):%d",[imgData length]);
like image 42
Suresh Varma Avatar answered Oct 21 '22 06:10

Suresh Varma