空的drawRect实现导致内存警告 [英] Empty drawRect implementation causes memory warning
问题描述
我有一个UIView,用手指可以在上面绘制UIBezierPath。
当我缩放视图时(例如在绘制路径之后),将触发重绘函数,该函数将重新缩放BezierPath:
I have a UIView on which I draw a UIBezierPath by finger. When I rezoom the view (say after a path is drawn) a redraw function is triggered, which rescales the BezierPath:
- (void)redrawPathsWithScale:(float)scale
{
[_path applyTransform:CGAffineTransformMakeScale(scale, scale)];
[self setNeedsDisplay];
}
setNeedsDisplay
导致 drawRect
被调用。
现在,每次我放大到x6附近的绝对比例时,我都会立即收到内存警告,并且应用程序崩溃。
setNeedsDisplay
causes drawRect
to get called.
Now every time I zoom in to a absolute scale somewhere near x6 I immediately get a memory warning, and the App crashes.
我的 drawRect
方法看起来像这样:
My drawRect
method looks like this:
- (void)drawRect:(CGRect)rect
{
[_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
}
奇怪的是:未实现 drawRect
完全删除了内存警告。实施空的 drawRect
仍会导致崩溃!
The Curious thing is: Not implementing drawRect
at all removes the memory warning. Implementing an empty drawRect
still causes a crash!
推荐答案
是否添加 [super drawRect:rect];
有什么区别吗?
Does adding [super drawRect:rect];
make any difference?
drawRect的Apple文档指出:
The Apple Documentation for drawRect states:
如果直接子类化UIView,则此方法的实现无需调用super。但是,如果要子类化其他视图类,则应在实现的某个时刻调用super。
If you subclass UIView directly, your implementation of this method does not need to call super. However, if you are subclassing a different view class, you should call super at some point in your implementation.
如果要子类化UIView,应该没问题,但为防万一,可能值得检查。
If you're subclassing UIView, you should be fine but it might be worth checking this just in case.
这篇关于空的drawRect实现导致内存警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!