I have a subclass of UIView which I draw a graph on it.
GraphingView.h
@interface GraphingView : UIView
...
@end
I draw things here:
GraphingView.m
- (void)drawRect:(CGRect)rect
{
...
}
Then, from another controller, I call this graph by just initializing and add that subview
graphView = [[GraphingView alloc] init];
...
[self.view addSubview:graphView];
But I actually call this subview multiple times with UIScrollView in different pages. Let's say 3 pages for 3 different graphs. Everything works fine if I just need 3 static graphs. Because of my scroller is infinite, every time I swipe the scroller left or right, the scroller will generate an older or newer graph which sits previous or next to the graph of direction. For that reason, I use scrollViewDidScroll
to detect and insert new graph. However, scrollViewDidScroll
makes too many calls to the subviews since it has not been stopped. So, that means I am re-drawing too much with Quartz 2D and it causes the app to crash. Does that sounds memory leak of Quartz?
For that matter, I then decided to call the graph subview in scrollViewDidEndDecelerating
instead. But after a while scrolling, the app crashes just like above since I still call the subview which has drawRect method too many times.
My question is, how do I prevent this Quartz from crashing because drawRect
is being called more than it should be?