我正在查看内存中的堆快照。这个函数似乎是废弃内存的罪魁祸首。
这是我的 View “MyView”之一的 View 构建代码的一部分。
如果我使用此函数创建和销毁“MyView”100 次,则注释内存大小始终返回到其基线。然而,如果我把这个功能留在我的内存中,它就会不断增加。
据我所知,我不拥有该函数中任何内容的所有权。 我做错了什么?
-(void)drawPointAroundCircle:(CGPoint)centerpoint radius:(int)radius amount:(int)amount
{
CGPoint pointarray[amount];
float thetaarray[7]={270.0,315.0,0.0,45.0,135,180.0,225.0};
int i=-1;
UIGraphicsBeginImageContext(CGSizeMake(self.frame.size.width,self.frame.size.height));
CGContextRef context=UIGraphicsGetCurrentContext();
for (i=0; i<7; i++) {
float x=cosf(D2R( thetaarray[i]))*radius;
float y =sinf( D2R(thetaarray[i]))*radius;
x=x+centerpoint.x;
y=y+centerpoint.y;
pointarray[i].x=x;
pointarray[i].y=y;
UIBezierPath *path=[UIBezierPath bezierPathWithArcCenter:CGPointMake(x, y) radius:2 startAngle:D2R(0) endAngle:D2R(360) clockwise:YES];
CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
[path fill];
[path closePath];
}
UIImage *bezierimage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *bezierImageView=[[UIImageView alloc]initWithImage:bezierimage];
[self addSubview:bezierImageView];
}
最佳答案
bezierImageview 在添加到 subview 之前应该自动释放,或者在添加到 subview 之后释放。使用当前代码,该对象的保留计数永远不会低于 1。
这是假设非 ARC,但如果您使用 ARC,则不会泄漏。
关于ios - 这是否导致了被遗弃的内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11472176/