简体   繁体   English

CALayer renderInContext:绘图的位置

[英]CALayer renderInContext: Position of Drawing

I have a UIView with a custom shape drawn in drawRect: . 我有一个UIView其中包含在drawRect:绘制的自定义形状drawRect: The frame property is set to: frame属性设置为:

{5.f, 6.f, 50.f, 50.f}

Now, I render the view in an Image Context, but it is ignoring the frame property of the UIView , and always drawing the UIView in the top left. 现在,我在图像上下文中渲染视图,但它忽略了UIView的frame属性,并且始终在左上角绘制UIView

[_shapeView.layer renderInContext:UIGraphicsGetCurrentContext()];

I tried to change the frame of the CALayer , but nothing changed. 我试图更改CALayerframe ,但没有任何改变。 Modifying the bounds property made things worse. 修改bounds属性会让事情变得更糟。 The only work around I found useful was: 我发现有用的唯一工作是:

CGRect frame = _shapeView.frame;
CGContextTranslateCTM(context, frame.origin.x, frame.origin.y);
[_shapeView.layer renderInContext:context];

But, this is impractical when I am dealing with many shapes, I want to just use the frame property. 但是,当我处理许多形状时,这是不切实际的,我想只使用frame属性。

Using CGContextTranslateCTM is the proper way to go. 使用CGContextTranslateCTM是正确的方法。 As renderInContext: documentation states : "Renders in the coordinate space of the layer." 作为renderInContext: documentation说明:“在图层的坐标空间中渲染”。 This means the frame origin of your layer/view is ignored. 这意味着将忽略图层/视图的框架原点。

Regarding CGContextDrawLayer , it is not made to be used with CALayer , but with CGLayer . 关于CGContextDrawLayer ,它不能与CALayer一起使用,而是与CGLayer一起CGLayer These are two very different things, and explains your crash. 这是两个截然不同的事情,并解释了你的崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM