2011-02-18 55 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