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
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.
UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage . Save this answer.
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.
Try the following code:
NSData *imageData = [[NSData alloc] initWithData:UIImageJPEGRepresentation((image), 1.0)];
int imageSize = imageData.length;
NSLog(@"SIZE OF IMAGE: %i ", imageSize);
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.
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]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With