2013-03-09 21 views
1

我正在繪製UIImage s到/來自電網。我目前只繪製的變化......而只有改變......但舊的圖像都應該留在原地......然而現在他們的drawRect:(CGRect)rect:如何繪製上下文而不會丟失?

- (void)drawRect:(CGRect)rect 
{ 
    int cellSize = self.bounds.size.width/WIDTH; 
    double xOffset = 0; 

    CGRect cellFrame = CGRectMake(0, 0, cellSize, cellSize); 
    NSUInteger cellIndex = 0; 
    cellFrame.origin.x = xOffset; 
    for (int i = 0; i < WIDTH; i++) 
    {   
     cellFrame.origin.y = 0; 
     for (int j = 0; j < HEIGHT; j++, cellIndex++) 
     { 
      if([[self.state.boardChanges objectAtIndex:(i*HEIGHT)+j] intValue]==1){ 
       if (CGRectIntersectsRect(rect, cellFrame))    { 
        NSNumber *currentCell = [self.state.board objectAtIndex:cellIndex]; 

        if (currentCell.intValue == 1) 
        { 
         [image1 drawInRect:cellFrame]; 
        } 
        else if (currentCell.intValue == 0) 
        { 
         [image2 drawInRect:cellFrame]; 
        } 
       } 
      } 
      cellFrame.origin.y += cellSize; 
     } 
     cellFrame.origin.x += cellSize; 
    } 
} 

我的下一個電話後消失嘗試了以下的混合物沒有任何結果:

回答

0

找到另一種解決方案 - >只需要一個截圖,並繪製drawRect作爲背景。

-(void)createContent{ 
    CGRect rect = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height); 
    UIGraphicsBeginImageContext(rect.size); 
    [self.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    CGImageRef screenshotRef = CGBitmapContextCreateImage(UIGraphicsGetCurrentContext()); 
    _backgroundImage = [[UIImage alloc] initWithCGImage:screenshotRef]; 
    CGImageRelease(screenshotRef); 
    UIGraphicsEndImageContext(); 
} 

- (void)drawRect:(CGRect)rect 
{ 
    [_backgroundImage drawInRect:CGRectMake(0.0f, 0.0f, self.bounds.size.width, self.bounds.size.height)]; 
    .... 
    .... 
    .... 
} 
###### EDIT

找到一個更爲複雜的方式做以上: http://www.cimgf.com/2009/02/03/record-your-core-animation-animation/

#### EDIT

另一種解決方案是使用CALayers,只有更新那些改變部分...

#### EDIT

OR:

self.layer.contents =(id)[_previousContent CGImage];

0

可可/ UIKit中可以每當它喜歡的丟棄的觀點先前繪製的內容。當你的視圖被要求繪製時,它必須繪製所提供的rect中的所有內容。你不能只是決定不畫東西。 (好吧,你可以做任何你喜歡的,但你得到的結果,你看到。)

相關問題