如何将UIView呈现为CGContext

SPa*_*til 9 iphone uiview cgcontext ipad ios

我想将UIView渲染成CGContextRef

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

    MagicalFunction(ctx, someView);
}
Run Code Online (Sandbox Code Playgroud)

因此,这里的MagicalFunction应该将UIView(可能是它的图层)渲染到当前上下文中.

我怎么做?

提前致谢!

Mat*_*ing 16

CALayer的renderInContext方法怎么样?

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

编辑:如评论中所述,由于过程中涉及的两个坐标系的起源不同,该图层将呈现倒置.要进行补偿,您只需垂直翻转上下文.这在技术上通过缩放和平移变换完成,可以在单个矩阵变换中组合:

-(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];
}
Run Code Online (Sandbox Code Playgroud)