Is there a way to transform a CGRect
with UIView
system coordinates into Core Graphics coordinates, where the origin is in the lower-left corner?
Asked
Active
Viewed 735 times
-1

Hamish
- 78,605
- 19
- 187
- 280

user2783443
- 91
- 1
- 9
-
http://stackoverflow.com/questions/506622/cgcontextdrawimage-draws-image-upside-down-when-passed-uiimage-cgimage/511199?s=1|0.0000#511199 uses the standard technique of moving to the height of the view and flipping the y-scale. CGContextScaleCTM(ctx, 1.0, -1.0); CGContextTranslateCTM(ctx, 0, -imageRect.size.height); – Glenn Howes Feb 10 '16 at 17:12
1 Answers
1
Sure. You just have to subtract the y-origin and the height of the rect away from the view's height.
rect.origin.y = view.frame.size.height-(rect.origin.y+rect.size.height)
You can represent this with a CGAffineTransform
like so:
CGAffineTransformMakeTranslation(0, view.size.height-((rect.origin.y*2.0)+rect.size.height))
You subtract the origin twice as you're now working with a relative value, instead of an absolute one.
However, if you only want to flip a context to work in UIView
coordinates you'd want:
CGFloat ctxHeight = CGContextGetClipBoundingBox(c).size.height;
CGContextScaleCTM(c, 1, -1);
CGContextTranslateCTM(c, 0, -ctxHeight);

Hamish
- 78,605
- 19
- 187
- 280