2010-07-23 49 views
1

我在其他地方发布了这个巴士无法获得帮助。DrawRect方法随时间推移减慢。为什么?

所以基本上我试图让它使用户可以“绘制”一个图像到视图上。我试图做到这一点的方式是通过屏幕上每9个像素的运动,我创建一个线的面具绘制,然后用该面具剪辑图像。

它实际上起初做工非常漂亮,然后经过这样的20秒左右的绘画,实在难以忍受。这不是离散的跳跃。它不断变得越来越慢。

我的想法是上下文不能正确清除,所以随着时间的推移,这种不正确的方法就不得不做更多的工作来裁剪图像。

无论如何,这是代码。

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
UITouch *touch = [touches anyObject]; 
CGPoint currentPoint = [touch locationInView:self.view]; 
currentPoint.y -= 20; 

CGRect dummyRect = CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height); 
UIGraphicsBeginImageContext(self.view.frame.size); 
CGContextRef context = UIGraphicsGetCurrentContext(); 
[realImage.image drawInRect:dummyRect]; 
CGContextSetLineCap(context, kCGLineCapRound); 
CGContextSetLineWidth(context, 20.0); 
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0); 
CGContextBeginPath(context); 
CGContextMoveToPoint(context, lastPoint.x, lastPoint.y); 
CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y); 
CGContextStrokePath(context); 
realImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
CGContextClearRect(context, dummyRect); 
UIGraphicsEndImageContext(); 




if(powf(startingPoint.x-currentPoint.x,2) + powf(startingPoint.y-currentPoint.y,2)>80) { 
    startingPoint = currentPoint; 

    CGRect rect = CGRectMake(0,0,320,480); 
    UIImage *dummyImage = [UIImage alloc]; 
    dummyImage = [self clipImage:[UIImage imageNamed:@"sauce.png"] withMask:realImage.image atRect:rect]; 

    UIImageView *dummyIV = [[UIImageView alloc] initWithImage:dummyImage]; 
    [self.view addSubview:dummyIV]; 
    [dummyIV release]; 

    realImage.image = nil; 
} 

而且我的修剪方法:

- (UIImage *)clipImage:(UIImage *)imageIn withMask:(UIImage *)maskIn atRect:(CGRect) maskRect { 
CGRect rect = CGRectMake(0, 0, imageIn.size.width, imageIn.size.height); 
CGImageRef msk = maskIn.CGImage; 

UIGraphicsBeginImageContext(imageIn.size); 
CGContextRef ctx = UIGraphicsGetCurrentContext(); 
CGContextClearRect(ctx, rect); 
CGContextClipToMask(ctx, maskRect, msk); 
CGContextTranslateCTM(ctx, 0.0, rect.size.height); 
CGContextScaleCTM(ctx, 1.0, -1.0); 
CGContextDrawImage(ctx, rect, imageIn.CGImage); 
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
CGContextClearRect(ctx, rect); 
UIGraphicsEndImageContext(); 

UIGraphicsBeginImageContext(maskRect.size); 
CGContextRef newctx = UIGraphicsGetCurrentContext(); 
CGRect clippedRect = CGRectMake(0, 0, maskRect.size.width, maskRect.size.height); 
CGContextClipToRect(newctx, clippedRect); 
CGRect drawRect = CGRectMake(maskRect.origin.x*-1, (newImage.size.height - maskRect.origin.y - maskRect.size.height)*-1, newImage.size.width, newImage.size.height); 
CGContextTranslateCTM(newctx, 0.0, 0); 
CGContextScaleCTM(newctx, 1.0, 1.0); 
CGContextDrawImage(newctx, drawRect, newImage.CGImage); 
UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext(); 
CGContextClearRect(newctx, clippedRect); 
UIGraphicsEndImageContext(); 

return cropped; 

感谢您的帮助!

回答

2

你一直在不断增加新的UIImages你的看法与此行

[self.view addSubview:dummyIV]; 

您添加的更多的意见,越慢变 - 每次在20秒后更新屏幕时都必须绘制每个图像,这可能有相当多的图像!

+0

我认为这是有道理的。这有什么办法呢? – Ryan 2010-07-23 11:57:07

+0

没关系。我只是做了一个imageview,并将图像重新绘制成一个视图。 你是对的。非常感谢! – Ryan 2010-07-23 12:21:47

0

貌似这条线是创建一些垃圾:

UIImage *dummyImage = [UIImage alloc]; 
+0

我已经添加了代码来释放该对象,但仍然没有改进。不过谢谢。 有没有其他想法? – Ryan 2010-07-23 11:26:32

相关问题