Reputation: 4399
I've been checking different solutions that I've found through StackOverflow but sadly I couldn't find the solution to this problem. I basically need to find the size (in bytes, Kb or whatever) to an image that is stored in the iPhone Image gallery. Actually, I need to validate the size before uploading it to a server.
So, I took a JPEG image from the internet:
http://static1.uk.businessinsider.com/image/596c82d34af3fa51058b4978-707/david%20slater.jpeg
Once I download this to my computer, when i get the file information it says: Size 80865 bytes (82 KB on disk). So, I save this file to the iPhone's image library. Once I get the image from the gallery and I inspect it through xCode I see the following:
So, basically the file has 80865 bytes when it's on my computers disk, but it has 289371 when I inspect it through xcode (the method length from the NSData object returns 'the number of bytes contained by the data object' according to the documentation). Am I missing something? Why is the image inside the iPhone disk almost 4 times the size of the image stored in the iPhone?
The code I am using is:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSUInteger length = [UIImageJPEGRepresentation(image,1.0f) length];
}
EDIT: Ok, so I've changed the code to save the image to disk first and check the size then, I use:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Image.png"];
[UIImageJPEGRepresentation(image,1.0f) writeToFile:filePath atomically:YES];
NSError *attributesError;
NSDictionary*fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&attributesError];
NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
}
So, If i print the value of the variable fileSizeNumber I get: 369055 (which according to the documentation indicated the file's size in bytes). But this number doesn't seem to match the original 80865 either, it is actually 10 times bigger than the original image!
Upvotes: 0
Views: 267
Reputation: 124997
[UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];
You're writing the uncompressed data (or at least differently compressed data) to a file and then checking its length. But the point the JPEG file format is that it compresses the data. It's not surprising that reading a JPEG image, writing it to a file as PNG data, and then looking at the length of that file doesn't tell you what you want.
I'm not sure why you don't believe what Xcode is telling you, but if you feel driven to see the actual size of the JPEG image in its compressed state, you'll need to find the JPEG file itself. You'd do something like:
[[NSBundle mainBundle] pathForResource:@"selfie" ofType:@"jpeg"];
to get a path to the file, and then use NSFileManager
to look at the attributes of the file at that path.
Upvotes: 1