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)];
        ....
        ....
        ....
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作