CALayer renderInContext:绘图的位置

Maz*_*yod 16 core-animation calayer cgcontext drawrect

我有一个UIView自定义形状绘制drawRect:.该frame属性设置为:

{5.f, 6.f, 50.f, 50.f}
Run Code Online (Sandbox Code Playgroud)

现在,我在图像上下文中渲染视图,但忽略了它的框架属性UIView,并始终UIView在左上角绘制.

[_shapeView.layer renderInContext:UIGraphicsGetCurrentContext()];
Run Code Online (Sandbox Code Playgroud)

我试图改变frameCALayer,但什么都没有改变.修改bounds财产使事情变得更糟.我发现有用的唯一工作是:

CGRect frame = _shapeView.frame;
CGContextTranslateCTM(context, frame.origin.x, frame.origin.y);
[_shapeView.layer renderInContext:context];
Run Code Online (Sandbox Code Playgroud)

但是,当我处理许多形状时,这是不切实际的,我想只使用frame属性.

ama*_*our 31

使用CGContextTranslateCTM是正确的方法.正如renderInContext: 文档所述:"在图层的坐标空间中渲染".这意味着将忽略图层/视图的框架原点.

关于CGContextDrawLayer,它不能与之一起使用CALayer,而是与之一起使用CGLayer.这是两个截然不同的事情,并解释了你的崩溃.