Iphone 代码中的内存管理问题 -(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件 { UITouch*touch=[触摸任何对象]; currentPoint=[触摸位置视图:self.view]; 根层=[CALayer层]; rootLayer.frame=self.view.bounds; [self.view.layer addSublayer:rootLayer]; starPath=CGPathCreateMutable(); CGPathMoveToPoint(starPath,NULL,currentPoint.x,currentPoint.y+15.0); 对于(int i=1;i
当我使用性能工具运行时,它会占用更多内存 当我搬家的时候 怎么办Iphone 代码中的内存管理问题 -(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件 { UITouch*touch=[触摸任何对象]; currentPoint=[触摸位置视图:self.view]; 根层=[CALayer层]; rootLayer.frame=self.view.bounds; [self.view.layer addSublayer:rootLayer]; starPath=CGPathCreateMutable(); CGPathMoveToPoint(starPath,NULL,currentPoint.x,currentPoint.y+15.0); 对于(int i=1;i,iphone,Iphone,当我使用性能工具运行时,它会占用更多内存 当我搬家的时候 怎么办 我需要在图层上的Touchs电影上绘制此星形,以便以后可以执行动画….您在dealloc中释放路径,但在您的TouchsMoved:中(可能)多次创建它。你可以安全地在你的代码结束时释放这个资源。TousEsTime: ,请考虑这样做。< /P> 另外,在您进行了更改之后,您可以从dealloc中删除该版本,因为您将不再需要在那里发布它。您的路径不存在于您的方法之外。感谢您的重播。。我也是这样做的。。。但在我的仪器中,当我搬家时,
我需要在图层上的Touchs电影上绘制此星形,以便以后可以执行动画….您在dealloc中释放路径,但在您的
TouchsMoved:
中(可能)多次创建它。你可以安全地在你的代码结束时释放这个资源。TousEsTime: ,请考虑这样做。< /P>
另外,在您进行了更改之后,您可以从dealloc中删除该版本,因为您将不再需要在那里发布它。您的路径不存在于您的方法之外。感谢您的重播。。我也是这样做的。。。但在我的仪器中,当我搬家时,所有的分配都增加了。。。。它不受控制……@Kiran为什么你把starpath声明为一个全局变量?杰尔的回答是正确的。您只需要释放starpath方法本身。
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[touches anyObject];
currentPoint=[touch locationInView:self.view];
rootLayer = [CALayer layer];
rootLayer.frame = self.view.bounds;
[self.view.layer addSublayer:rootLayer];
starPath = CGPathCreateMutable();
CGPathMoveToPoint(starPath, NULL, currentPoint.x, currentPoint.y + 15.0);
for(int i = 1; i < 5; ++i)
{
CGFloat x = 15.0 * sinf(i * 4.0 * M_PI / 5.0);
CGFloat y = 15.0 * cosf(i * 4.0 * M_PI / 5.0);
CGPathAddLineToPoint(starPath, NULL, currentPoint.x + x, currentPoint.y + y);
}
CGPathCloseSubpath(starPath);
shapeLayer = [CAShapeLayer layer];
shapeLayer.path = starPath;
UIColor *fillColor = [UIColor colorWithWhite:0.9 alpha:1.0];
shapeLayer.fillColor = fillColor.CGColor;
[rootLayer addSublayer:shapeLayer];
}
- (void)dealloc {
[imageView release];
CGPathRelease(starPath);
[super dealloc];
}