将CALayer绘制到CGContext中

| 我有以下代码:
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSaveGState(context);

CALayer *sublayer = [CALayer layer];
sublayer.backgroundColor = [UIColor orangeColor].CGColor;
sublayer.cornerRadius = 20.0;
sublayer.frame = CGRectMake(20, 0, 300, 20);

[sublayer setNeedsDisplay];
[sublayer drawInContext:context];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return newImage;
但是,当我查看返回的newImage时,只有一个空图像。当我将drawInContext更改为renderInContext时,我得到了上面的子层,但似乎坐标系混乱了。 任何想法为什么上面的drawInContext不起作用?     
已邀请:
尝试使用
renderInContext
代替
drawInContext
。 我的理解是
drawInContext
应该被覆盖,而
renderInContext
是用来将层的内容呈现到上下文中的。 从文档中:   
- drawInContext:
     此方法的默认实现本身不会进行任何绘制。如果图层的委托实现
drawLayer:inContext:
方法,则将调用该方法进行实际绘制。      子类可以重写此方法,并使用它来绘制图层的内容。绘制时,所有坐标应在逻辑坐标空间中的点中指定。   
- renderInContext:
     此方法直接从图层树进行渲染,而忽略添加到渲染树的所有动画。在图层的坐标空间中渲染。     
坐标系本身并没有弄乱。 Quartz使用与UIKit不同的坐标系。在Quartz中,Y轴起源于框架矩形的左下角。当您沿矩形“上”行得越远,这些值就会变得越大。有关视觉表示的信息,请参见以下文档: http://developer.apple.com/library/mac/#documentation/graphicsimaging/Conceptual/drawingwithquartz2d/dq_overview/dq_overview.html 这与UIKit的不同之处在于,UIKit的坐标系原点位于左上角,y轴值在您“向下”移动时会变得更正。 至于为什么drawInContext:不起作用,您还应该引用CALayer类的文档,其中说“默认实现什么都不做。” http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/CALayer_class/Introduction/Introduction.html     

要回复问题请先登录注册