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
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
UIImage+CalculatedSize.m
You simply import the
UIImage+CalculatedSize.h
and use it like this:Or, if you want to avoid using categories:
EDIT:
This calculation of course has nothing to do with JPEG/PNG compression. It relates to underlaying CGimage:
In a way a size retrieved this way gives you a worst-case scenario information without actually creating an expensive additional object.
Try the following code:
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 useUIImagePNGRepresentation
orUIImageJPEGRepresentation
to get the equivalentNSData
constructs for either, then check the size of that.From:@Meet's answer: