Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display image size in MB format IOS 7

I tying to check my image size after picking form the photo library by using this code.

 NSData *imageData1 = [[NSData alloc] initWithData:UIImageJPEGRepresentation((imageview.image), 0.5)];

   int imageSize = imageData1.length;
   NSLog(@"SIZE OF IMAGE: %i ", imageSize);

But showing something like this .

SIZE OF IMAGE: 237125 

But i want to view the image size in the MB format like 25 MB how to do pls tell me how to do it.

thanks.

like image 767
user3349668 Avatar asked Feb 26 '14 09:02

user3349668


3 Answers

[NSByteCountFormatter stringFromByteCount:imageSize 
                      countStyle:NSByteCountFormatterCountStyleFile];
like image 179
rounak Avatar answered Nov 09 '22 09:11

rounak


imageSize has data length in bytes. You need to convert it to Mb

NSLog(@"SIZE OF IMAGE: %i Mb", imageSize/1024/1024);

UPDATE:

You can also use following code

NSLog(@"SIZE OF IMAGE: %.2f Mb", (float)imageSize/1024/1024);

to receive output like

SIZE OF IMAGE: 0.23 Mb

for sizes less the 1 Mb.

like image 39
Avt Avatar answered Nov 09 '22 07:11

Avt


Complementing @rounak 's answer, in Swift 3:

ByteCountFormatter.string(fromByteCount: Int64(imageSize), countStyle: .file)
like image 1
Gianni Carlo Avatar answered Nov 09 '22 07:11

Gianni Carlo