Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get image file size which selected by UIImagePickerController?

I want to know that the image file size in IPhone PhotoAlbum which selected by UIImagePickerController.

I've tried this code with 1,571,299 byte jpeg image.

UIIamge *selectedImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

NSData *imageData; 
if ( /* PNG IMAGE */ ) 
    imageData = UIImagePNGReprensentation(selectedImage);
else 
    imageData = UIImageJPEGReprensentation(selectedImage);

NSUInteger fileLength = [imageData length];

NSLog(@"file length : [%u]", fileLength);

But when I run the code, it print 362788 byte.

Is there anybody who know this?

like image 269
James.Jo Avatar asked Sep 29 '11 05:09

James.Jo


2 Answers

If you have code like this to take a picture:

UIImagePickerController *controller = [[[UIImagePickerController alloc] init] autorelease];
controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
controller.delegate = self;
[self presentModalViewController:controller animated:YES];

Then you can retrieve a file size of the picked image in the following way:

NSURL *assetURL = [info objectForKey:@"UIImagePickerControllerReferenceURL"];
ALAssetsLibrary *library = [[[ALAssetsLibrary alloc] init] autorelease];
[library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
    NSLog(@"Size: %lld", asset.defaultRepresentation.size);
} failureBlock:nil];

If the source type is UIImagePickerControllerSourceTypeCamera, you must save the in-memory image to disk before retrieving its file size.

like image 93
Vadim Avatar answered Nov 01 '22 06:11

Vadim


As some commenters have said, even if we assume the methodology is correct you are reprocessing the image anyway so the byte sizes will not match. I use the following method for JPG images, ymmv for PNG:

+ (NSInteger)bytesInImage:(UIImage *)image {
    CGImageRef imageRef = [image CGImage];
    return CGImageGetBytesPerRow(imageRef) * CGImageGetHeight(imageRef);    
}

The above, as a commenter noted, does return the uncompressed size however.

like image 37
Diziet Avatar answered Nov 01 '22 06:11

Diziet