我想将一个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];
}
编辑: 如评论中所述,由于涉及到过程中两个坐标系统的起源差异,图层将会被倒置渲染。为了补偿这一点,您只需要垂直翻转上下文即可。技术上,可以使用缩放和平移变换来完成此操作,并将它们组合成单个矩阵变换:
-(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];
}
drawRect
无关。如果您已经重写了drawRect
以在视图中进行一些自定义绘制,则它将按原样呈现到上下文中。 - Matt Wilding