1

我有这段代码在 iPad 上运行非常缓慢(由于某种原因,在 iPhone 上运行更快):

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

[self.navigationController setNavigationBarHidden:YES animated:NO];

mouseSwiped = YES;
UITouch *touch = [touches anyObject];   
currentPoint = [touch locationInView:frontgroundView];

CGContextBeginPath(context);
CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
CGContextStrokePath(context);

maskRef = CGBitmapContextCreateImage(context);  

CGImageRef masked = CGImageCreateWithMask([backgroundImage CGImage], maskRef);
CGImageRelease(maskRef);

CGContextRef drawContext = UIGraphicsGetCurrentContext();

CGContextSaveGState(drawContext);
CGContextTranslateCTM (drawContext,0,frontgroundView.frame.size.height);
CGContextScaleCTM (drawContext, 1,-1);

CGContextDrawImage(drawContext, CGRectMake(0, 0, frontgroundView.frame.size.width, frontgroundView.frame.size.height), [frontgroundView.image CGImage]);

CGContextRestoreGState (drawContext);

CGContextDrawImage(drawContext, CGRectMake(0, 0, frontgroundView.frame.size.width, frontgroundView.frame.size.height), masked);

CGImageRef finalImage = CGBitmapContextCreateImage(drawContext);
frontgroundView.image = [UIImage imageWithCGImage:finalImage];
CGImageRelease(finalImage);

CGImageRelease(masked);

lastPoint = currentPoint;

mouseMoved++;

if (mouseMoved == 10)
    mouseMoved = 0;
}

有任何想法吗??

4

1 回答 1

0

您应该在 -drawRect: 中为您的视图进行绘图,而不是直接响应触摸。

于 2011-04-01T21:34:19.563 回答