Question

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.

Was it helpful?

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;

OTHER TIPS

The units are points, which translate to 1pt == 1px in non-retina and 1pt == 2px in retina.

If you want integral values, you can use CGRectIntegral like:

view.frame = CGRectIntegral(view.frame)

Points Versus Pixels In iOS, all coordinate values and distances are specified using floating-point values in units referred to as points. The measurable size of a point varies from device to device and is largely irrelevant. The main thing to understand about points is that they provide a fixed frame of reference for drawing.

https://developer.apple.com/library/ios/documentation/windowsviews/conceptual/viewpg_iphoneos/WindowsandViews/WindowsandViews.html

https://developer.apple.com/library/ios/documentation/general/conceptual/Devpedia-CocoaApp/CoordinateSystem.html

YOu can go through the above documentation in detail

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top