我想将 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];
}
关于iphone - 如何将 UIView 渲染到 CGContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5041403/