Reputation: 91
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?
Upvotes: 9
Views: 5444
Reputation: 4387
here is in Swift 4
extension UIImage {
var memorySize: Int {
guard let imageRef = self.cgImage else { return 0 }
return imageRef.bytesPerRow * imageRef.height
}
}
Upvotes: 0
Reputation: 9493
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.
Upvotes: 6
Reputation: 2417
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.
Upvotes: 2