tags:

views:

81

answers:

3
brushSize=1.0f;
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapButt); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brushSize);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 1.0, 0.0, 1.0);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x+brushSize*4,lastPoint.y-brushSize);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x-brushSize, lastPoint.y+brushSize*4);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

I am using this Code for drawing......In touchesmoved.

i do getting gaps when iam drawing fastly... what to do?

A: 

You should use UITouch's previousLocationInView: method to get the previous known point you finger was and draw a line between that point and the current location.

Another solution is to keep the currentPoint in a static variable between two calls.

VdesmedT
@vdsmedT.... CGPoint currentPoint=[touch previousLocationInView:self.view]; if i used this in touchesMoved it not changed ....
kiran
CGPoint prevPoint = [touch previousLocationInView:self.view];CGPoint curPoint = [touch locationInView:self.view];
VdesmedT
@vdsmedT.... I used previouslocationInView too....... still i do getting gaps when iam drawing.
kiran
@kiran: Try the other solution then. Keep the current point in a static variable as a starting point for the next touchesMouved call.
VdesmedT
+1  A: 
CGContextBeginPath(Mycontext);
CGContextMoveToPoint(Mycontext, lastPoint.x, lastPoint.y);

int x, cx, deltax, xstep,y, cy, deltay, ystep,error, st, dupe;
int x0, y0, x1, y1;

x0 = currentPoint.x;
y0 = currentPoint.y;
x1 = lastPoint.x;
y1 = lastPoint.y;

// find largest delta for pixel steps
st = (abs(y1 - y0) > abs(x1 - x0));

// if deltay > deltax then swap x,y
if (st) {
(x0 ^= y0); (y0 ^= x0); (x0 ^= y0); // swap(x0, y0);
(x1 ^= y1); (y1 ^= x1); (x1 ^= y1); // swap(x1, y1);
}

deltax = abs(x1 - x0);
deltay = abs(y1 - y0);
error = (deltax / 2);
y = y0;

if (x0 > x1) { xstep = -1; }
else { xstep = 1; }

if (y0 > y1) { ystep = -1; }
else { ystep = 1; }

for ((x = x0); (x != (x1 + xstep)); (x += xstep))
{
(cx = x); (cy = y); // copy of x, copy of y

// if x,y swapped above, swap them back now
if (st) { (cx ^= cy); (cy ^= cx); (cx ^= cy); }

(dupe = 0); // initialize no dupe

if(!dupe) {
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), cx+brushSize*4,cy-brushSize);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), cx-brushSize, cy+brushSize*4);

}

(error -= deltay); // converge toward end of line

if (error < 0) { // not done yet
(y += ystep);
(error += deltax);
}
}

CGContextStrokePath(Mycontext);
Image_Cookie.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

lastPoint = currentPoint;
Ashish Mathur
@brad larson....... Thanks .... Can we still optimize it....
kiran
A: 
  • (void)viewDidLoad { [super viewDidLoad]; drawImage = [[UIImageView alloc] initWithImage:nil]; drawImage.frame = self.view.frame; self.view.backgroundColor=[UIColor whiteColor]; [self.view addSubview:drawImage];

} -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject];
currentPoint=[touch locationInView:self.view]; lastPoint=[touch previousLocationInView:self.view]; lastPoint.y -= 20; [self drawShape];} -(void)drawShape{
brushSize=2; UIGraphicsBeginImageContext(self.view.frame.size); [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapSquare); CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0); CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x+brushSize*4,lastPoint.y-brushSize); CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x-brushSize, lastPoint.y+brushSize*4); CGContextStrokePath(UIGraphicsGetCurrentContext()); CGContextFlush(UIGraphicsGetCurrentContext()); drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); lastPoint=currentPoint; } I used This code to draw....... but i get gap ...... when i drawing...... what to do... where to change....

kiran