ios - 通过 renderInContext : 定位用于绘制的 View

标签 ios core-graphics calayer cgcontext

我想在我当前的 CGGraphicsContext 中绘制一个 UIView。我通过 renderInContext: 绘制了 UIView,但它的位置不正确(总是在左上角)。

我拥有可用于绘制 UIViewUIView 的所有值。

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

我目前这样设置图层的位置和旋转:

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

但由于旋转,位置不正确,与原始位置不同。
我怎样才能恢复原来的位置?坐标系在两者之间没有改变,只是将 center.xcenter.y 值转换为适合 CGContextTranslateCTM 的值的问题>.

编辑:
保存的值是正确的,保存正确的boundscentertransform 不是我的问题的根源,我的问题的根源是通过 CGContextTranslateCTM 将这些值设置为可绘制的 CGLayer

最佳答案

可以使用以下代码片段将图形上下文的几何形状调整为 UIView 的几何形状:

// 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);

关于ios - 通过 renderInContext : 定位用于绘制的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23110601/

相关文章:

ios - 如何确定注释是否在 MKPolygonView 内部(iOS)

core-graphics - 合并核心图形中的路径?

ios - 以灰度渲染下面的所有 UIView

ios - 绘制多条线 Core Graphics

swift - 如何将之字形边框添加到圆形 ImageView

iOS - 单击按钮时更改 CALayer 的大小然后返回到原始大小

ios - 带有暂存 Assets 的应用商店提交

ios - 将表格单元格委托(delegate)给其他表格 View

html - 在 ios/safari/iPhone 上设置 HTML 选择标签的样式

iphone - 我如何更改 Core Graphics 中 CALayer 的填充颜色?