I am developing an application where I am drawing in drawRect method of a customClass.
I draw lines in the Graphics context of the View. When I redraw them again and again using setNeedsDisplay, I get memory warnings and my App immediately crashes.
I checked if there were any leaks in my drawRect code. I found nothing. The memory allocation also did not show any major difference.
The problem got fixed once, the iOS device was rebooted. But I am sure the crash will be repeated again. What could be the problem. Have any of you faced similar problems?
The code is below:
- (void)drawRect:(CGRect)rect{
[self drawTheTimeLineHorizontally];
}
- (void) drawTheTimeLineHorizontally {
//Get the current graphics context
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSaveGState(currentContext);
[UIColorFromRGB(kCalendarTimeLineColor) setStroke];
CGContextSetLineWidth(currentContext, 1);
CGMutablePathRef path = CGPathCreateMutable();
NSArray *hours = [self currentZoomLevelIntervalList];
int numHours = [hours count];
for (int i = 0; i < numHours; ++i) {
CGPathMoveToPoint(path, NULL, 0, (i+kMultiplierTopDailyCalendarTimeline)*offset+2);
[self drawHoursLeftOfLines:[hours objectAtIndex:i] withContext:currentContext withRect:CGRectMake(kOriginXOfTextInTimeLine, (i+kMultiplierTopDailyCalendarTimeline)*offset+(offset/3), kWidthOfTextInTimeLine, offset/3)];
[UIColorFromRGB(kCalendarTimeLineColor) setStroke];
CGPathAddLineToPoint(path, NULL, widthOfDailyCalendar+orginXEventTile, ((i+kMultiplierTopDailyCalendarTimeline)*offset+2));
}
CGPathMoveToPoint(path, NULL, 0, (numHours+kMultiplierTopDailyCalendarTimeline)*offset+2);
CGPathAddLineToPoint(path, NULL, widthOfDailyCalendar+orginXEventTile, (numHours+kMultiplierTopDailyCalendarTimeline)*offset+2);
CGContextAddPath(currentContext, path);
CGContextDrawPath(currentContext, kCGPathStroke);
//CGContextClosePath(currentContext);
CGPathRelease(path);
//Restore the saved context
CGContextRestoreGState(currentContext);
}
- (void) drawHoursLeftOfLines:(NSString*) time withContext:(CGContextRef) context withRect:(CGRect) contextRect {
[UIColorFromRGB(kTimeLineHourTextColor) setStroke];
CGContextSelectFont(context, kTimeLineHourTextFontStyle , kFontSizeTimeLineText, kCGEncodingMacRoman);
CGContextSetCharacterSpacing (context, 1);
CGContextSetTextDrawingMode(context, kCGTextFillStroke);
CGAffineTransform xform = CGAffineTransformMake(
1.0, 0.0,
0.0, -1.0,
0.0, 0.0);
CGContextSetTextMatrix(context, xform);
CGContextShowTextAtPoint(context, contextRect.origin.x, contextRect.origin.y, [time cStringUsingEncoding:NSASCIIStringEncoding], [time length]);
}
UPDATE:
The crash got repeated again in the same flow. This happened after over 8 hours after the device was rebooted. I have not used the App for whole 8 hours. After I rebooted the device, the Application does not crash in that Particular flow at all.