user523234
user523234

Reputation: 14834

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?

Upvotes: 4

Views: 3809

Answers (2)

Alex Nichol
Alex Nichol

Reputation: 7510

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.

Upvotes: 2

Philippe Leybaert
Philippe Leybaert

Reputation: 171914

Do you mean the width/height?

CGFloat width = image.size.width
CGFloat height = image.size.height

Upvotes: 6

Related Questions