2013-03-09 34 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中的所有内容。你不能只是决定不画东西。 (好吧,你可以做任何你喜欢的,但你得到的结果,你看到。)

相关问题