【问题标题】:UITableView addsubview crahesUITableView addsubview 崩溃
【发布时间】:2012-10-21 16:13:38
【问题描述】:

我想将包含 CABasicAnimation 的自定义 UIView 添加到 UITableViewCell。我这样做是通过以下方式

if( self.pie_chart_view == nil )
    self.pie_chart_view = [[PieChartView alloc] initWithFrame:CGRectMake( 0.f, 0.f, 30.f, 30.f )];
[pie_chart_view animate_progress_from:0.0 to:1.0 with_duration:5.0];
[cell.contentView addSubview:pie_chart_view];

但代码崩溃,如下图所示:

崩溃显然是由以下代码引起的

- (void)drawInContext:(CGContextRef)context {
CGRect circleRect = CGRectInset(self.bounds, 1, 1);

CGColorRef borderColor = [[UIColor whiteColor] CGColor];
CGColorRef backgroundColor = [[UIColor colorWithWhite:0 alpha: 0.75] CGColor];

CGContextSetFillColorWithColor(context, backgroundColor); <--- CRASHES ON THIS LINE WITH EXC_BAD_ACCESS
CGContextSetStrokeColorWithColor(context, borderColor);
CGContextSetLineWidth(context, 2.0f);

CGContextFillEllipseInRect(context, circleRect);
CGContextStrokeEllipseInRect(context, circleRect);

CGFloat radius = MIN(CGRectGetMidX(circleRect), CGRectGetMidY(circleRect));
CGPoint center = CGPointMake(radius, CGRectGetMidY(circleRect));
CGFloat startAngle = -M_PI / 2;
CGFloat endAngle = self.progress * 2 * M_PI + startAngle;
CGContextSetFillColorWithColor(context, borderColor);
CGContextMoveToPoint(context, center.x, center.y);
CGContextAddArc(context, center.x, center.y, radius, startAngle, endAngle, 0);
CGContextClosePath(context);
CGContextFillPath(context);

[super drawInContext:context];
}

但我不明白它为什么会崩溃。我的项目使用 ARC。我在这里错过了什么?

【问题讨论】:

    标签: uitableview subview caanimation


    【解决方案1】:

    好的,我解决了这个问题。使用 ARC 时,UIColor 会立即被删除。可以通过执行以下操作来修复此问题:

    UIColor * __autoreleasing borderColor = [UIColor whiteColor];
    UIColor * __autoreleasing backgroundColor = [UIColor colorWithWhite:0 alpha: 0.75];
    
    CGContextSetFillColorWithColor(context, backgroundColor.CGColor );
    CGContextSetStrokeColorWithColor(context, borderColor.CGColor );
    

    之后代码不再崩溃。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 2018-07-15
      • 1970-01-01
      • 2011-08-19
      • 2011-08-08
      • 1970-01-01
      相关资源
      最近更新 更多