在下面的代码中执行“CGContextFillRect”语句后,我看到内存使用量急剧增加(在 iPad 上从 39 MB 增加到 186 MB)。这里有什么问题吗。
我的应用程序最终崩溃了。
PS:令人惊讶的是,内存峰值出现在第 3 代和第 4 代 iPad 上,而不是在第 2 代 iPad 上。
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setBackgroundColor:[UIColor clearColor]];
}
return self;
}
- (id)initWithFrame:(CGRect)iFrame andHollowCircles:(NSArray *)iCircles {
self = [super initWithFrame:iFrame];
if (self) {
[self setBackgroundColor:[UIColor clearColor]];
self.circleViews = iCircles;
}
return self;
}
- (void)drawHollowPoint:(CGPoint)iHollowPoint withRadius:(NSNumber *)iRadius {
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(currentContext, self.circleRadius.floatValue);
[[UIColor whiteColor] setFill];
CGContextAddArc(currentContext, iHollowPoint.x, iHollowPoint.y, iRadius.floatValue, 0, M_PI * 2, YES);
CGContextFillPath(currentContext);
}
- (void)drawRect:(CGRect)rect {
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSaveGState(currentContext);
CGRect aRect = [self.superview bounds];
[[UIColor whiteColor]setFill];
CGContextFillRect(currentContext, aRect);
CGContextSaveGState(currentContext);
[[UIColor blackColor]setFill];
CGContextFillRect(currentContext, aRect);
CGContextRestoreGState(currentContext);
for (MyCircleView *circleView in self.circleViews) {
[self drawHollowPoint:circleView.center withRadius:circleView.circleRadius];
}
CGContextTranslateCTM(currentContext, 0, self.bounds.size.height);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextSaveGState(currentContext);
}