Web上の画像の高さ、幅を取得する。

iOSアプリで、Web上から画像を取得してきます。
そのときに画像の高さと幅の値を
objective-cのライブラリを使用して
取得する方法を示します。

以下にそのコードを示します。


  1. NSData *imageData = /*画像のデータ*/
  2. UIImage *pre_image = [[UIImage alloc]initWithData:imageData]; //UIImage型に変換
  3. UIImageView *image = [[UIImageView alloc]initWithImage:pre_image]; //UIImageView型にセット
  4. [image setFrame:CGRectMake(0, 0, 320, 480)];
  5. [image sizeToFit];
  6. NSLog(@"%f,%f",image.frame.size.height,image.frame.size.width);


Web上から取得した画像データは、
まずimageDataの変数に入っているものとします。

各処理を経て最終的に
image.frame.size.heightが目的の画像の高さであり、
image.frame.size.widthが幅です。