【发布时间】:2012-08-14 02:55:15
【问题描述】:
我的应用程序出现内存警告,然后它崩溃了。错误,我认为是在我的触摸绘图功能上,因为当我尝试使用 alloc 工具运行它时,当我继续在屏幕上移动手指时,内存开始增加。
这是我的代码。
touchSwiped = YES;
UITouch *touch = [touches anyObject];
previousPoint2 = previousPoint1;
previousPoint1 = currentTouch;
currentTouch = [touch locationInView:self.view];
CGPoint mid1 = midPoint(previousPoint2, previousPoint1);
CGPoint mid2 = midPoint(currentTouch, previousPoint1);
UIGraphicsBeginImageContext(CGSizeMake(480 , 320));
[drawView.image drawInRect:CGRectMake(0, 0, 480,320)];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineCap(context,kCGLineCapRound);
CGContextSetLineWidth(context, inkWidth);
CGContextSetBlendMode(context, blendMode);
CGContextSetRGBStrokeColor(context,redColor, greenColor, blueColor, 1);
CGContextBeginPath(context);
CGContextMoveToPoint(context, mid1.x, mid1.y);
CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
CGContextStrokePath(context);
drawView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsGetCurrentContext();
【问题讨论】:
-
您为每次触摸/拖动创建了很多东西,即使使用 ARC,我相信您必须释放 CGContextRef 和其他一些东西。我不完全确定,因此这是一个评论。祝你好运!
-
你说对了一半,@ohr。 Core Graphics 和 Core Foundation “对象”不是由 ARC 管理的内存,但是您从
UIGraphicsGetCurrentContext()获得的CGContextRef不属于调用者(该函数不属于 havecopyorcreatein its name),并且没有被释放。 -
@ohr 成功了!!谢谢!
标签: objective-c ios memory automatic-ref-counting