开发者

objective-c determine image resolution

开发者 https://www.devze.com 2023-03-26 11:13 出处:网络
For some开发者_StackOverflow社区 reason I could not figure it out. UIImage *image = //initialize with some image, etc.

For some开发者_StackOverflow社区 reason I could not figure it out.

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

Is there a way to get the resolution of image?


Do you mean the width/height?

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号