Search code examples
iosiphonecocos2d-iphone

Drawing Issue with ACEDrawingView


I want to implement fast forward functionality using ACEDrawing. What I am doing is when user is drawing any drawing at that time I record all points from which used draw a line. So after user finished its Drawing, I will have all points from which used draw line. So I want develop fast forward functionality in which user will click on one button to see the final drawing. I have implemented code as shown below. It works fine for Small drawing that is for say 2000 points. But When points increases to 1000000 and above it crashed. I don't know how it handle this. Can any buddy give suggestion on this?

Code:

    self.currentTool = ACEDrawingToolTypePen;
    self.currentTool.lineWidth = self.lineWidth;
    self.currentTool.lineColor = [UIColor blackColor];
    self.currentTool.lineAlpha = 1;
    [self.currentTool setInitialPoint:currentPoint];
    [self.pathArray addObject:self.currentTool];
    currentPointArray = [mainDictionary objectForKey:@"points"];
    for (int j = 2; j<[currentPointArray count]; j++) {

         currentPoint = CGPointMake([[[currentPointArray objectAtIndex:j] objectForKey:@"x"] floatValue]*widMul, [[[currentPointArray objectAtIndex:j] objectForKey:@"y"] floatValue]*heiMul);

         previousPoint2 = CGPointMake([[[currentPointArray objectAtIndex:j-2] objectForKey:@"x"] floatValue]*widMul, [[[currentPointArray objectAtIndex:j-2] objectForKey:@"y"] floatValue]*heiMul);
         previousPoint1 = CGPointMake([[[currentPointArray objectAtIndex:j-1] objectForKey:@"x"] floatValue]*widMul, [[[currentPointArray objectAtIndex:j-1] objectForKey:@"y"] floatValue]*heiMul);
         if ([self.currentTool isKindOfClass:[ACEDrawingPenTool class]]) {

              CGRect bounds = [(ACEDrawingPenTool*)self.currentTool addPathPreviousPreviousPoint:previousPoint2 withPreviousPoint:previousPoint1 withCurrentPoint:currentPoint];
              CGRect drawBox = bounds;
              drawBox.origin.x -= self.lineWidth * 1.0;
              drawBox.size.width += self.lineWidth * 2.0;
              drawBox.size.height += self.lineWidth * 2.0;
              [self setNeedsDisplayInRect:drawBox];
        }
        [self updateCacheImage:NO];
}

Solution

  • It seems you're having memory issues.

    You can change your code as follows. In your updateCacheImage method, you need to put your imageContext in AutoReleaspool to free up your memory.

    @autoreleasepool {
    
        // init a context
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
    
        //End Context
        UIGraphicsEndImageContext();
    }
    

    You also have to set your objects to nil for making your memory free, like this:

    id object;
    object = nil;