0
votes

so I have a [NSMutableArray *paths] that contains UIBezierPaths that correspond to each separate path drawn on the screen. I also have another NSMutable array colors that contains the corresponding color for the path. The problem that I am having is that although the two arrays are mutating and I can see the count of both going up, the following drawRect method only seems to draw the last path in the array. I have scoured through a lot of questions and based on what I have read so far, this very problem seems to get us newbies and mostly because of type mismatch. I even tried to Typecast the return and it was futile. Here is my code from the drawRect

 int cnt = paths.count; 

if (cnt != 0)
{
    for(int i = 0; i < cnt; i++)
    {
        NSLog(@"Drawing path:%i", i);

        UIColor *color;

        color = [colors objectAtIndex:i];
        [color setStroke];


        UIBezierPath *path = [paths objectAtIndex:i];
        path.lineWidth = 2;

        [path stroke];
    }
}

every time I call [self setNeedsDisplay] it wipes out my entire screen.

Am I correct in assuming that I am not handling my

UIBezierPath *path = [paths objectAtIndex:i];

well?

Thank you very much and in advance.

UPDATE:

I was rummaging through some more codes and now it almost looks like there is another possibility that ARC is releasing the contents of my NSMutable array. I tried the do the following and got a compiler error.

[paths retain];
1
Well, being the hardheaded person I am, I realized that in a sense, I had no need to use NSMutable arrays for what I was trying to do. Until I need to implement an "undo feature" I will stick to having three UIBezierPaths (red, blue and green) and a currentPath pointer that points to one of the three. This way, when it is time to draw, I can simply check to see if each of the paths is nil and otherwise draw the paths. To be easier on the memory, I am considering implementing a filter so that a path will only be redrawn if it has changed since the last render. - Khanal

1 Answers

0
votes

Besure you add your path to your path array afer each draw

- (void)drawRect:(CGRect)rect {

    [brushPattern setStroke];
    UIBezierPath *drawPath = [UIBezierPath bezierPath];
    drawPath.lineCapStyle = kCGLineCapRound;
    drawPath.miterLimit = 0;
    drawPath.lineWidth = thisLineWidth;

    for (UIBezierPath *path in pathArray)
        [drawPath appendPath:path];

    UIBezierPath *path = [self pathForCurrentLine];
    if (path) 
        [drawPath appendPath:path];


    [drawPath stroke];


}

- (UIBezierPath*)pathForCurrentLine {


    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:startPoint];
    [path addLineToPoint:endPoint];

    return path;

}