0

ビットマップコンテキストでiPadの指でペイントするこのコードがあります。そして、実線ではなく線(長い点)の線が表示され、指を動かすと線がちらつき、指を動かすと画面の両側に2本の線が表示されるという意味で2倍になります。何が起こっているのですか?

前もって感謝します。

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
  UITouch *touch = [touches anyObject];

self.prePreviousPoint = self.previousPoint;
self.previousPoint = [touch previousLocationInView:self];
CGPoint currentPoint = [touch locationInView:self];

// calculate mid point
CGPoint mid1 = [self calculateMidPointForPoint:self.previousPoint andPoint:self.prePreviousPoint];
CGPoint mid2 = [self calculateMidPointForPoint:currentPoint andPoint:self.previousPoint];

//inicijaliziranje contexta u kojem se sve crta
UIGraphicsBeginImageContext(self.drawImageView.frame.size);
// CGContextRef context = UIGraphicsGetCurrentContext();

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
context = CGBitmapContextCreate(NULL, self.drawImageView.frame.size.width, self.drawImageView.frame.size.height, 8, self.drawImageView.frame.size.width*4, colorSpace,kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);
UIGraphicsPushContext(context);
CGRect rect = CGRectMake(0, 0, self.drawImageView.frame.size.width, self.drawImageView.frame.size.height);

// setting line color

[[self currentColor] setStroke];

CGContextSetAllowsAntialiasing(context, true);
CGContextSetShouldAntialias(context, true);

//inicijaliziranje viewa u koji se crta slika te odredivanje vrha linije i biljezenje pokreta
[self.drawImageView.image drawInRect:rect];
CGContextMoveToPoint(context, mid1.x, mid1.y);
// Use QuadCurve is the key
CGContextAddQuadCurveToPoint(context, self.previousPoint.x, self.previousPoint.y, mid2.x, mid2.y);

CGContextSetLineCap(context, kCGLineCapRound);

// complete distance variable is used for setting line width in respect of speed of finger movment and time the finger moves

CGFloat xDist = (previousPoint.x - currentPoint.x); //[2]
CGFloat yDist = (previousPoint.y - currentPoint.y); //[3]
CGFloat distance = sqrt((xDist * xDist) + (yDist * yDist)); //[4]

distance = distance / 10;

if (distance > 10) {
    distance = 10.0;
}

distance = distance / 10;
distance = distance * 3;

if (4.0 - distance > self.lineWidth) {
    lineWidth = lineWidth + 0.3;
} else {
    lineWidth = lineWidth - 0.3;
}

CGContextSetLineWidth(context, self.lineWidth);

//    CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);

NSLog(@"%@",context);
//Crtanje svega na ekran i "zatvaranje" image contexta
//CGContextDrawImage(context,CGRectMake(0, 0, self.drawImageView.frame.size.width, self.drawImageView.frame.size.height) , image1);
CGContextStrokePath(context);

CGImageRef image = CGBitmapContextCreateImage(context);
image2 = [UIImage imageWithCGImage:image];
//UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
self.drawImageView.image = image2;
UIGraphicsPopContext();
CGImageRelease(image);
NSLog(@"%@ %@",image,image2);
CGContextRelease(context);
UIGraphicsEndImageContext();

}

4

1 に答える 1

0
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
 colorSpace = CGColorSpaceCreateDeviceRGB();
context1 = CGBitmapContextCreate(NULL, self.drawImageView.frame.size.width, self.drawImageView.frame.size.height, 8, self.drawImageView.frame.size.width*4, colorSpace,kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);

self.previousPoint = [touch locationInView:self];
}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
 UITouch *touch = [touches anyObject];

self.prePreviousPoint = self.previousPoint;
self.previousPoint = [touch previousLocationInView:self];
CGPoint currentPoint = [touch locationInView:self];

// calculate mid point
CGPoint mid1 = [self calculateMidPointForPoint:self.previousPoint andPoint:self.prePreviousPoint];
CGPoint mid2 = [self calculateMidPointForPoint:currentPoint andPoint:self.previousPoint];

rect = CGRectMake(0, 0, self.drawImageView.frame.size.width, self.drawImageView.frame.size.height);

    UIGraphicsPushContext(context1);

  UIGraphicsBeginImageContext(self.drawImageView.frame.size);
  context = UIGraphicsGetCurrentContext();


  // setting line color

[[self currentColor] setStroke];

CGContextSetAllowsAntialiasing(context, true);
CGContextSetShouldAntialias(context, true);

[self.drawImageView.image drawInRect:rect];

CGContextMoveToPoint(context, mid1.x, mid1.y);
// Use QuadCurve is the key
CGContextAddQuadCurveToPoint(context, self.previousPoint.x, self.previousPoint.y, mid2.x, mid2.y);

 CGContextSetLineCap(context, kCGLineCapRound);

 // complete distance variable is used for setting line width in respect of speed of finger    movment and time the finger moves

CGFloat xDist = (previousPoint.x - currentPoint.x); //[2]
CGFloat yDist = (previousPoint.y - currentPoint.y); //[3]
CGFloat distance = sqrt((xDist * xDist) + (yDist * yDist)); //[4]

distance = distance / 10;

if (distance > 10) {
    distance = 10.0;
}

distance = distance / 10;
distance = distance * 3;

if (4.0 - distance > self.lineWidth) {
    lineWidth = lineWidth + 0.3;
} else {
    lineWidth = lineWidth - 0.3;
}

CGContextSetLineWidth(context, 15);

//    CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);

 CGContextStrokePath(context);

 image = CGBitmapContextCreateImage(context);
 image2 = [UIImage imageWithCGImage:image];

UIGraphicsPopContext();
UIGraphicsEndImageContext();
CGContextRelease(context1);
CGImageRelease(image);
[drawImageView setImage:image2];

}

これは私の問題を解決したコード全体で、最後に自分で見つけました。

于 2012-09-03T18:08:32.160 に答える