【问题标题】:DrawRect method slowing over time. Why?DrawRect 方法会随着时间的推移而变慢。为什么?
【发布时间】:2010-07-23 11:18:35
【问题描述】:

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

所以基本上我正在尝试让用户可以将图像“绘制”到视图上。我试图实现这一点的方法是通过屏幕上每 9 个像素的移动,我创建一个绘制线条的蒙版,然后使用该蒙版剪辑图像。

其实一开始效果很好,然后像这样画了大约 20 秒后,它的延迟令人难以忍受。这不是离散的跳跃。它不断变得越来越慢。

我的想法是上下文没有被正确清除,所以随着时间的推移,drawrect 方法不得不做更多的工作来剪辑图像。

无论如何,这是代码。

- (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;

感谢您的帮助!

【问题讨论】:

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


    【解决方案1】:

    你不断地用这条线向你的视图添加新的 UIImages

    [self.view addSubview:dummyIV];
    

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

    【讨论】:

    • 我认为这是有道理的。有什么方法可以解决这个问题?
    • 没关系。我刚刚制作了一个图像视图并将图像重新绘制到那个视图中。你完全正确。非常感谢!
    【解决方案2】:

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

    UIImage *dummyImage = [UIImage alloc];
    

    【讨论】:

    • 我已经添加了代码来发布该对象,但仍然没有改进。不过谢谢。还有其他想法吗?
    猜你喜欢
    • 2014-02-04
    • 1970-01-01
    • 2021-05-02
    • 2018-06-25
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 2014-07-17
    • 2021-11-29
    相关资源
    最近更新 更多