IOS get jpg file width

In my iOS application, I download jpeg images from the Internet, and I am wondering how to find the correct width and height of the image so that I can display it correctly in my iOS application.

For instance,
enter image description here

How to get the width and height of this jpg image?

+6
source share
3 answers

you can get the height and width of the image, for example: -

NSData *imageData = [NSData dataWithContentsOfURL:imageUrl]; UIImage *image = [UIImage imageWithData:imageData]; NSLog(@'image height: %f',image.size.height); NSLog(@'image width: %f',image.size.width); 

UPDATE: -

according to your url you can get: -

 NSString *ImageURL = @"http://gigaom2.files.wordpress.com/2013/01/teacher-classroom.jpg"; ImageURL =[ImageURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; NSLog(@"img url ==%@",ImageURL); NSURL *imageUrl =[NSURL URLWithString:ImageURL]; NSData *imageData = [NSData dataWithContentsOfURL:imageUrl]; UIImage *image = [UIImage imageWithData:imageData]; [inView setImage:image]; NSLog(@"image height: %f",image.size.height); [lbl1 setText:[NSString stringWithFormat:@"%2.0f",image.size.height]]; [lbl2 setText:[NSString stringWithFormat:@"%2.0f",image.size.width]]; 

your screen looks like this: -

enter image description here

I just create a demo for you :) link below.

http://www.sendspace.com/file/7pp63a

+22
source

UIImage has a size property

 image.size.width image.size.height 
+4
source

You can achieve this using the UIImage Size property.

Take a look below:

 UIImage *image; NSLog(@'Width=%f and Height: %f',image.size.width, image.size.height); 

Store your web image in the image object and you will get the width and height of the image.

Hooray!

+3
source

All Articles