Question

I want to draw a UIView in my current CGGraphicsContext. I draw the UIView via renderInContext:, but it's not positioned correctly (always in the top left corner).

I have all values of the UIView available for drawing the UIView.

CGRect frame;
CGRect bound;
CGPoint center;
CGAffineTransform transform;

I currently set the position and rotation of the layer like this:

CGContextTranslateCTM(context, frame.origin.x, frame.origin.y);
CGContextRotateCTM(context, (CGFloat) atan2(transform.b, transform.a));

But the position is, due to the rotation, not correct and differs from the original position.
How can I restore the original position? The coordinate system hasn't changed in between, it's just an issue to translate the center.x and center.y values into something appropriate for CGContextTranslateCTM.

Edit:
The saved values are correct, saving the correct bounds, center and transform is not the origin of my issue, the origin of my issue, is setting theses values to the drawable CGLayer via CGContextTranslateCTM.

Was it helpful?

Solution

The geometry of the graphics context can be adjusted to the UIViews geometry with this snippet:

// Center the context around the view's anchor point
CGContextTranslateCTM(context, [view center].x, [view center].y);
// Apply the view's transform about the anchor point
CGContextConcatCTM(context, [view transform]);
// Offset by the portion of the bounds left of and above the anchor point
CGContextTranslateCTM(context,
                      -[view bounds].size.width * [[view layer] anchorPoint].x,
                      -[view bounds].size.height * [[view layer] anchorPoint].y);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top