DrawRect方法随着时间的推移而减慢。为什么?

时间:2010-07-23 11:18:35

标签: iphone objective-c quartz-graphics quartz-2d

我在其他地方发布的公共汽车无法获得帮助。

所以基本上我正在尝试制作它,以便用户可以将“绘制”图像到视图上。我试图完成此操作的方法是在屏幕上每移动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 个答案:

答案 0 :(得分:2)

您不断使用此行向视图中添加新的UIImages

[self.view addSubview:dummyIV];

您添加的视图越多,速度越慢 - 每次更新屏幕时都必须绘制每个图像,因此在20秒后,可能会有相当多的图像!

答案 1 :(得分:0)

看起来这一行正在创造一些垃圾:

UIImage *dummyImage = [UIImage alloc];