Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

objective-c determine image resolution

For some reason I could not figure it out.

UIImage *image = //initialize with some image, etc.

Is there a way to get the resolution of image?

like image 650
user523234 Avatar asked Feb 24 '23 08:02

user523234


2 Answers

To find the dimensions of the image, you can use the size property on a UIImage. The DPI (resolution/scale) can be found through the scale attribute. Here is a reference from Apple's documentation:

The scale factor of the image. (read-only)

@property (nonatomic,readonly) CGFloat scale

Discussion

If you load an image from a file whose name includes the @2x modifier, the scale is set to 2.0. If the filename does not include the modifier but is in the PNG or JPEG format and has an associated DPI value, a corresponding scale factor is computed and reflected in this property. You can also specify an explicit scale factor when initializing an image from a Core Graphics image. All other images are assumed to have a scale factor of 1.0.

If you multiply the logical size of the image (stored in the size property) by the value in this property, you get the dimensions of the image in pixels.

For normal images, the scale will be 1.0. For JPG/PNG images, the scale may be different. Also, if your image name has '@2x' before the extension, UIImage assumes that its scale is 2.0.

like image 32
Alex Nichol Avatar answered Feb 28 '23 17:02

Alex Nichol


Do you mean the width/height?

CGFloat width = image.size.width
CGFloat height = image.size.height
like image 121
Philippe Leybaert Avatar answered Feb 28 '23 19:02

Philippe Leybaert