I do not think it is possible to change CG to use Pixel Coordinates instead of Point Coordinates. There are a number of scaling and rasterization options for CALayer but I do not think they will do what you want.
Here is a programmatic solution that uses an extension so you can do a CGRect() style:
let rectInPixels = CGRect(pointsX: 50.0, y: 50.0, width: 100.0, height: 100.0)
Results:
rectInPixels: (100.0, 100.0, 200.0, 200.0)
Only difference is you use "pointsX:" instead of "x:" when creating a rect. Of course, you can use whatever argument name that makes most sense to you.
Or convert a rect from points to pixels:
//Create a rect using Point coordinates
let rectInPoints = CGRect(x: 50.0, y: 50.0, width: 100.0, height: 100.0)
//Convert from Points to Pixel coordinates
let rectInPixels = rectInPoints.pixelCoords
Results:
rectInPixels: (100.0, 100.0, 200.0, 200.0)
Of course you can make a similar extension for CGPoint or any other datatype.
Extension:
public extension CGRect {
init(fromPointCoords pointRect: CGRect) {
let res = UIScreen.main.nativeScale
self.init(x: pointRect.origin.x * res, y: pointRect.origin.y * res, width: pointRect.size.width * res, height: pointRect.size.height * res)
}
init(pointsX x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat) {
let rectPixels = CGRect(fromPointCoords: CGRect(x, y, width, height))
self.init(origin: rectPixels.origin, size: rectPixels.size)
}
var pixelCoords:CGRect {
get {
return CGRect(fromPointCoords: self)
}
set(newValue) {}
}
}