Search code examples
ioscoordinate-systems

In iOS coordinate system, is the given location in pixels or in another specific unit?


In my application, the user can manually place some UIViews inside a UIImageView. While processing the result, I noticed that UIViews Location (X, Y) or Width or even Height are floats so I'm having some floats values like for example: 100.22233

I don't understand how it could be possible because I need to have those values in pixels, is there any way to convert those values to pixels?

Thanks.


Solution

  • UIView properties such as frame and bounds provide coordinates in points, not pixels. One point is one pixel on a non-retina screen; one point is four pixels (2x2 rect) on a retina screen.

    If you want integer values for your coordinates, you can truncate float values like this:

    CGFloat xLocation = (NSInteger)100.22233 + (CGFloat)0.0f;
    

    If you instead want to round to the nearest integer (101 when starting with 100.8, as you suggested in comments), you can add 0.5 to the float before rounding it, like this:

    CGFloat xLocation = (NSInteger)(100.8 + 0.5) + (CGFloat)0.0f;