漏网画

时间:2014-08-20 19:16:31

标签: ios objective-c uiview memory-leaks drawrect

我需要将网格绘制到UIViewControllers.view。由于某种原因,我的例程分配了大量的内存(15到20兆字节),并且它没有正确释放它。如果我以编程方式刷新网格,我的应用程序会分配越来越多的内存,最终会崩溃。在崩溃之前,它会收到许多didReceiveMemoryWarnings。如果我注释掉以下代码应用程序按预期工作。

- (void)drawRect:(CGRect)rect {
    [self.lineColor setStroke];
    NSInteger horizontalLineScaling = roundf((float)(self.bounds.size.height-30)/((float)(self.endTime-self.startTime)*4));
    // horizontal lines
    CGFloat dash1[] = {2.0, 0, 2.0};
    UIBezierPath *horizontal1 = [[UIBezierPath alloc] init];
    [horizontal1 setLineDash:dash1 count:1 phase:2];
    [horizontal1 setLineWidth:1];
    for (float y = horizontalLineScaling; y < self.bounds.size.height; y += horizontalLineScaling*2) {
        [horizontal1 moveToPoint:CGPointMake(1, y+30)];
        [horizontal1 addLineToPoint:CGPointMake(self.bounds.size.width, y+30)];
    };

    UIBezierPath *horizontal2 = [[UIBezierPath alloc] init];
    [horizontal2 setLineWidth:1];
    for (float y = 0; y < self.bounds.size.height; y += horizontalLineScaling*2) {
        [horizontal2 moveToPoint:CGPointMake(1, y+30)];
        [horizontal2 addLineToPoint:CGPointMake(self.bounds.size.width, y+30)];
    };

    UIBezierPath *horizontal3 = [[UIBezierPath alloc] init];
    [horizontal3 setLineWidth:3];
    for (float y = 0; y < self.bounds.size.height; y += horizontalLineScaling*4) {
        [horizontal3 moveToPoint:CGPointMake(1, y+30)];
        [horizontal3 addLineToPoint:CGPointMake(self.bounds.size.width, y+30)];
    };
    // Frame
    UIBezierPath *screenFrame = [UIBezierPath bezierPathWithRect:CGRectMake(self.bounds.origin.x, self.bounds.origin.y+30, self.bounds.size.width, self.bounds.size.height)];
    // Vertical Lines
    UIBezierPath *vertical = [[UIBezierPath alloc] init];
    [vertical setLineWidth:2];
    [vertical moveToPoint:CGPointMake(self.bounds.size.width-5, 0)];
    [vertical addLineToPoint:CGPointMake(self.bounds.size.width-5, 29)];
    [vertical moveToPoint:CGPointMake(self.bounds.size.width-2, 0)];
    [vertical addLineToPoint:CGPointMake(self.bounds.size.width-2, 29)];

    [horizontal1 stroke];
    [horizontal2 stroke];
    [horizontal3 stroke];
    [screenFrame stroke];
    [vertical stroke];
}

有没有办法在没有内存问题的情况下绘制网格?

0 个答案:

没有答案