How to display a UIView in a CGContext

I wanted to show a UIView in a CGContextRef

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

So, the MagicalFunction here should map the UIView (maybe its layer) to the current context.

How to do it?

Thanks in advance!

+8
ios iphone ipad uiview cgcontext
source share
1 answer

What about the renderInContext CALayer method?

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

EDIT: As noted in the comment, due to the difference in origin in the two coordinate systems involved in the process, the layer will be flipped. To compensate, you just need to flip the context vertically. This is technically performed with a scale and transform transformation that can be combined into a single matrix transformation:

 -(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]; } 
+12
source share

All Articles