2011-02-18 36 views
8

我想呈现一个UIView到CGContextRef如何呈现的UIView成CGContext上

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

    MagicalFunction(ctx, someView); 
} 

所以,这里的MagicalFunction应该呈现的UIView(可能是其层)为当前上下文。

我该怎么做?

在此先感谢!

回答

12

renderInContext方法CALayer怎么样?

-(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]; 
} 
+0

谢谢!但是这些观点是颠倒的!有什么建议么? – SPatil 2011-02-19 06:56:45