0

I have this code to paint with my finger on ipad in bitmap context. And i'm getting lined (long dots) line instead of full line and the line is flickering when i move my finger, and it doubled in sense that when i move my finger i get two lines on each side of the screen. Any idea what is going on?

Thanks in advance.

-(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];

}

This is the entire code that solved my problem i figured it out on my own at the end.

于 2012-09-03T18:08:32.160 回答