How to render UIView into a CGContext

五迷三道 提交于 2019-12-08 16:10:23

问题


I Wanted to render a UIView into a CGContextRef

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

    MagicalFunction(ctx, someView);
}

So, the MagicalFunction here is supposed to render the UIView(may be its layer) into current context.

How do I do that?

Thanks in advance!


回答1:


How about the renderInContext method of CALayer?

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

EDIT: As noted in the comment, due to a difference in origins in the two coordinate systems involved in the process, the layer will be rendered upside-down. To compensate, you just need to flip the context vertically. This is technically done with a scale and translation transformation, which can be combined in 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];
}


来源:https://stackoverflow.com/questions/5041403/how-to-render-uiview-into-a-cgcontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!