如何将UIView呈现为CGContext [英] How to render UIView into a CGContext

查看:177
本文介绍了如何将UIView呈现为CGContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将UIView渲染成CGContextRef

I Wanted to render a UIView into a CGContextRef

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

    MagicalFunction(ctx, someView);
}

所以,这里的MagicalFunction应该呈现UIView(可能是它的层)进入当前环境。

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

我该怎么做?

提前致谢!

推荐答案

CALayer

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

编辑:正如评论中所述,到期对于过程中涉及的两个坐标系中的原点差异,该层将被颠倒呈现。要进行补偿,您只需垂直翻转上下文。这在技术上通过缩放和转换转换完成,可以在单个矩阵转换中组合:

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];
}

这篇关于如何将UIView呈现为CGContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆