私はこれを他の場所に投稿しましたが、バスは助けを得ることができませんでした。
つまり、基本的には、ユーザーが画像をビューに「描画」できるようにしようとしています。私がこれを達成しようとしている方法は、画面上で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;
助けてくれてありがとう!