Display image size in MB format IOS 7
Asked Answered
T

3

8

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.

Tola answered 26/2, 2014 at 9:58 Comment(0)
P
8

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.

Piperonal answered 26/2, 2014 at 10:0 Comment(4)
now its showing 0mb in console pls tell me how to relsolve thisTola
It is because 237125 bytes is less then 1 Mb.Piperonal
i have checked with some high size image but its still showing 0 mb onlyTola
Probably it was also less then 1 Mb. Please try second variant from my answer.Piperonal
K
15
[NSByteCountFormatter stringFromByteCount:imageSize 
                      countStyle:NSByteCountFormatterCountStyleFile];
Kutch answered 26/2, 2014 at 10:3 Comment(1)
Yup, this should be marked as the correct answer, I added my own with a version in SwiftLapsus
P
8

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.

Piperonal answered 26/2, 2014 at 10:0 Comment(4)
now its showing 0mb in console pls tell me how to relsolve thisTola
It is because 237125 bytes is less then 1 Mb.Piperonal
i have checked with some high size image but its still showing 0 mb onlyTola
Probably it was also less then 1 Mb. Please try second variant from my answer.Piperonal
L
1

Complementing @rounak 's answer, in Swift 3:

ByteCountFormatter.string(fromByteCount: Int64(imageSize), countStyle: .file)
Lapsus answered 7/4, 2017 at 16:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.