yue1liang1chuan 2013-03-14 02:18 采纳率: 0%
浏览 2007

绘制context不影响以前的

正在从grid绘制UIImage,目前在绘制修改。原来的图片应该不会受影响,但是现在调用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;
    }   
}

试过下面的代码但是没有结果:

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    //CGContextAddRect(context, originalRect);
    CGContextClip(context);    
    [image drawInRect:rect];
    CGContextRestoreGState(context);
  • 写回答

1条回答

  • Kill_it 2013-03-14 08:12
    关注

    有一个解决方法,做一个屏幕截图,然后用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)];
        ....
        ....
        ....
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站