iOS get width height of jpg file

爷,独闯天下 提交于 2020-01-01 03:18:27

问题


In my iOS app, I'm downloading jpeg images from the web, and I'm wondering how to find the correct width and height of the image so I can display it properly in my iOS app.

For example,

How do I get the width and height of this jpg image?


回答1:


you can get Image Height and Width like:-

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:-

as per your URL you can get like:-

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 look like:-

I just Create a Demo for You :) link is bellow

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




回答2:


UIImage has the property size

image.size.width
image.size.height



回答3:


You can achieve this using UIImage Size property.

Have a look at below:

UIImage *image;

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

Store your web image into "image" object and you will get Width & Height of the image.

Cheers!



来源:https://stackoverflow.com/questions/14620279/ios-get-width-height-of-jpg-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!