Question

I have a very large UIView approx 3000x3000 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 1000x1000 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.

Was it helpful?

Solution

you can use

- (void)setNeedsDisplayInRect:(CGRect)invalidRect

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

+(Class)layerClass
{
return [CATiledLayer class];
}

to your view in scrollView

OTHER TIPS

Just to make that clear, a 3000x3000pixel UIView has a CALayer behind (it does not matter a lot if tiled or not) which uses 3000x3000x4 bytes space, so that is approx 34-35MB. You won't see that memory usage in the app itself, as it is allocated in the springboard.

The best solution from my point of view would be to have tiled layers with a layer just a little bit larger than your view (because they can render off thread), then have clipping enabled, and assign your paths with an appropriate offset to the layers. That is quite some work to do, but would work quite well.

Ah, somtimes, bevor you do the redraw it helps to clear the CALayers cache. A layer.contents = nil would do that....

cheers, Jörg

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top