如何将UIView渲染成CGContext

我想要渲染一个UIView到CGContextRef

-(void)methodName:(CGContextRef)ctx { UIView *someView = [[UIView alloc] init]; MagicalFunction(ctx, someView); } 

所以,这里的MagicalFunction应该把UIView(可能是它的图层)渲染到当前的上下文中。

我怎么做?

提前致谢!

如何CALayer的renderInContext方法?

 -(void)methodName:(CGContextRef)ctx { UIView *someView = [[UIView alloc] init]; [someView.layer renderInContext:ctx]; } 

编辑:正如在评论中指出的那样,由于在这个过程中所涉及的两个坐标系的起源有所不同,这个图层将被颠倒渲染。 为了弥补,你只需要垂直翻转上下文。 这在技术上是通过缩放和平移转换来完成的,它们可以组合在一个单一的matrix转换中:

 -(void)methodName:(CGContextRef)ctx { UIView *someView = [[UIView alloc] init]; CGAffineTransform verticalFlip = CGAffineTransformMake(1, 0, 0, -1, 0, someView.frame.size.height); CGContextConcatCTM(ctx, verticalFlip); [someView.layer renderInContext:ctx]; }