我正在向网格绘制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);
答案 0 :(得分:0)
Cocoa / UIKit可能会随时丢弃以前绘制的视图内容。当您要求绘制视图时,它必须在提供的rect
内绘制所有内容。你不能只是决定不画一些东西。 (好吧,你可以做任何你喜欢的事,但是你得到了你所看到的结果。)
答案 1 :(得分:0)
找到另一种解决方案 - &gt;只需截取屏幕截图并以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)];
....
....
....
}
######编辑
找到了一种更复杂的方法来完成上述工作: http://www.cimgf.com/2009/02/03/record-your-core-animation-animation/
####编辑另一种解决方案是使用CALayers并仅更新那些更改的部分......
####编辑OR:
self.layer.contents =(id)[_ previousContent CGImage];