简体   繁体   English

iOS获取jpg文件的宽度高度

[英]iOS get width height of jpg file

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. 在我的iOS应用程序中,我正在从网上下载jpeg图像,我想知道如何找到图像的正确宽度和高度,以便我可以在我的iOS应用程序中正确显示它。

For example, 例如,
在此输入图像描述

How do I get the width and height of this jpg image? 如何获得此jpg图像的宽度和高度?

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 http://www.sendspace.com/file/7pp63a

UIImage has the property size UIImage拥有房产size

image.size.width
image.size.height

You can achieve this using UIImage Size property. 您可以使用UIImage Size属性实现此目的。

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. 将您的Web图像存储到“图像”对象中,您将获得图像的宽度和高度。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM