2010-10-26 57 views
1

我使用UIGraphicsPushContext推上下文中的drawRect像這樣做:環境中使用UIGraphicsPushContext不顯示渲染圖像

- (void)drawRect:(CGRect)rect { 
    // Create Bitmap Graphics Context 
    UIContextRef ctxt = //blah blah; 
    // Push the custom context in the graphics stack 
    UIGraphicsPushContext(ctxt); 
    // Draw Stuff 
    // Get Image from custom context 
} 

我的問題是圖像不顯示在屏幕上(上圖),但我可以確認我已成功繪製上下文,因爲從上下文中檢索的圖像顯示的圖像與我打算繪製的圖像相似。

我想實現的是取代我的UIGraphicsGetCurrentContext()(它工作並顯示圖像)的調用,並使用(推送)我可以使用的自定義上下文。

我在這裏錯過了什麼嗎?謝謝!

+0

究竟是什麼,你要完成?如果你想繪製到視圖,你應該使用UIGraphicsGetCurrentContext()。 – loomer 2010-10-26 13:32:47

+0

我想將它繪製在自定義上下文中,而不是從drawRect給予我的上下文,以便我可以控制位圖內存並重用相同的上下文。 – LaN 2010-10-27 02:20:09

回答

0

如果要繪製圖像然後在視圖中顯示,則必須在視圖上繪製圖像,然後再繪製圖像,或先繪製圖像然後在視圖上繪製該圖像。爲了實現第二個選項做這樣的事情:

- (void)drawRect:(CGRect)rect { 
    // Create image 
    UIGraphicsBeginImageContext(rect.size); 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 

    // Draw the image content using ctx 

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    // Draw image on view 
    [image drawAtPoint:CGPointZero]; 
} 
+0

嗯,我知道我可以做到這一點,這不能回答我的問題。而且,高像素密度屏幕上的drawAtPoint和CGContextDrawImage往往會變慢。無論如何感謝:) – LaN 2010-10-28 03:46:54

+0

好吧,那麼我看到的唯一解決方案是首先繪製視圖,然後再繪製圖像上下文。但它可能也是這樣,我仍然不明白你的問題:) – loomer 2010-10-28 08:33:50

0

我想你錯過這樣的:

- (void)drawRect:(CGRect)rect { 
    // Create Bitmap Graphics Context 
    UIContextRef ctxt = //blah blah; 
    // Push the custom context in the graphics stack 
    UIGraphicsPushContext(ctxt); 
    // Draw Stuff 
    // Get Image from custom context 

    //blah blah 

    UIGraphicsPopContext(); 
} 

使用UIGraphicsPopContext()即可恢復到以前的上下文