I posted this elsewhere bus was unable to get help.
So basically I'm trying to make it so the user can "draw" an Image onto a view. The way I'm trying to accomplish this is by every 9ish pixel of movement on the screen, I create a mask of the line drawn and then clip the image with that mask.
It actually works beautifully at first, and then after about 20 seconds of drawing like this, it is unbearably laggy. It's not a discrete jump. It continuously gets slower and slower.
My thought is the the context is not getting cleared correctly, so with time, the drawrect method is having to do more work to clip the image.
Anyways, here's the code.
- (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;
}
Also my clip method:
- (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;
Thanks for any help!