I have a very large UIView approx 3000×3000 in size. In this large view I do some free form drawing with stylus or finger on an iPad. Here is my code for my touches began and moved methods.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
Line_Point * to_be_added = [[Line_Point alloc] init];
to_be_added.line_color = [UIColor colorWithCGColor: current_color.CGColor];
to_be_added.line_pattern = [NSString stringWithString: current_line_pattern];
UITouch *touch = [[event touchesForView:self] anyObject];
CGPoint location = [touch locationInView:self];
to_be_added.point = [NSValue valueWithCGPoint:(location)];
if (self.points == nil)
{
NSMutableArray *newPoints = [[NSMutableArray alloc] init];
self.points = newPoints;
[newPoints release];
}
[self.points addObject:to_be_added];
}
Here is my touches moved method which seems to be causing the issue because it isnt getting called enough because the app calls setNeedsDisplay
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
Line_Point * to_be_added = [[Line_Point alloc] init];
to_be_added.line_color = [UIColor colorWithCGColor: current_color.CGColor];
to_be_added.line_pattern = [NSString stringWithString: current_line_pattern];
UITouch * touch = [[event touchesForView:self] anyObject];
CGPoint location = [touch locationInView:self];
to_be_added.point = [NSValue valueWithCGPoint:(location)];
[self.points addObject:to_be_added];
[self setNeedsLayout];
}
Now here is my drawRect method which displays the drawing
-(void)drawRect:(CGRect)rect
{
if (self.points.count == 0)
{
}
else
{
Line_Point * first = [self.points objectAtIndex:0];
CGContextRef context2 = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context2, first.line_color.CGColor);
if ([first.line_pattern isEqualToString:@"normal"])
{
CGContextSetLineWidth(context2, 4.0);
CGContextSetLineDash(context2, 0, NULL, 0);
}
else if([first.line_pattern isEqualToString:@"thick"])
{
CGContextSetLineWidth(context2, 6.0);
CGContextSetLineDash(context2, 0, NULL, 0);
}
else if([first.line_pattern isEqualToString:@"dotted"])
{
CGFloat Pattern[] = {5, 5, 5, 5};
CGContextSetLineDash(context2, 0, Pattern, 4);
}
else if([first.line_pattern isEqualToString:@"super_dotted"])
{
CGFloat Pattern[] = {1, 2, 1, 2};
CGContextSetLineDash(context2, 0, Pattern, 4);
}
CGPoint firstPoint2 = [first.point CGPointValue];
CGContextBeginPath(context2);
CGContextMoveToPoint(context2, firstPoint2.x, firstPoint2.y);
int i2 = 1;
while (i2 < self.points.count)
{
Line_Point * nextPoint = [self.points objectAtIndex:i2];
if (nextPoint.point.CGPointValue.x < 0 && nextPoint.point.CGPointValue.y < 0)
{
CGContextDrawPath(context2, kCGPathStroke);
if (i2 < (self.points.count-1))
{
CGContextBeginPath(context2);
Line_Point * nextPoint2 = [self.points objectAtIndex:i2+1];
CGContextMoveToPoint(context2, nextPoint2.point.CGPointValue.x, nextPoint2.point.CGPointValue.y);
CGContextSetStrokeColorWithColor(context2, nextPoint2.line_color.CGColor);
if ([nextPoint2.line_pattern isEqualToString:@"normal"])
{
CGContextSetLineWidth(context2, 4.0);
CGContextSetLineDash(context2, 0, NULL, 0);
}
else if([nextPoint2.line_pattern isEqualToString:@"thick"])
{
CGContextSetLineWidth(context2, 6.0);
CGContextSetLineDash(context2, 0, NULL, 0);
}
else if([nextPoint2.line_pattern isEqualToString:@"dotted"])
{
CGFloat Pattern[] = {5, 5, 5, 5};
CGContextSetLineDash(context2, 0, Pattern, 4);
}
else if([nextPoint2.line_pattern isEqualToString:@"super_dotted"])
{
CGFloat Pattern[] = {1, 2, 1, 2};
CGContextSetLineDash(context2, 0, Pattern, 4);
}
i2 = i2 + 2;
}
else
i2++;
}
else
{
CGContextAddLineToPoint(context2, nextPoint.point.CGPointValue.x, nextPoint.point.CGPointValue.y);
i2++;
}
}
CGContextDrawPath(context2, kCGPathStroke);
}
The app lags incredibly when drawing in the free form mode. I believe it is because the view is too large and setNeedsDisplay is reloading the entire view every time the touches moved method is called. Is there a way to speed up the setNeedsDisplay method so the free form drawing does not lag on the iPad. Can i just target the context where the drawing is occurring not reload the whole thing? When I adjust the size to about 1000×1000 it does not have the lag problem.
Thanks for any help on the matter.
EDIT TRYED USING
-SetNeedsDisplayinRect
The UIView is wrapped inside a UISCrollview and here is the code.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
Line_Point * to_be_added = [[Line_Point alloc] init];
to_be_added.line_color = [UIColor colorWithCGColor: current_color.CGColor];
to_be_added.line_pattern = [NSString stringWithString: current_line_pattern];
UITouch * touch = [[event touchesForView:self] anyObject];
CGPoint location = [touch locationInView:self];
to_be_added.point = [NSValue valueWithCGPoint:(location)];
[self.points addObject:to_be_added];
CGRect visibleRect;
visibleRect.origin = big_view.scrollable_view.contentOffset;
visibleRect.size = CGSizeMake(950, 500);
[self setNeedsDisplayInRect:visibleRect];
}
This method of approach does not help either.
Thanks again if you can help.
you can use
instead of -setNeedsDisplay
another solution (if there is too many points and the app lags because of drawing them) – not to render all points every time when -drawInRect is called. You can, for example, at every 10-th call of it save image from current context and then display only image.
Update:
try adding
to your view in scrollView