2010-07-23 57 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

相關問題