我在其他地方发布了这个巴士无法获得帮助。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;
感谢您的帮助!
我认为这是有道理的。这有什么办法呢? – Ryan 2010-07-23 11:57:07
没关系。我只是做了一个imageview,并将图像重新绘制成一个视图。 你是对的。非常感谢! – Ryan 2010-07-23 12:21:47