CGContextRef - EXC_BAD_ACCESS错误[复制](CGContextRef

2019-09-22 02:03发布

这个问题已经在这里有一个答案:

  • 分配现有CGColor到CGColor财产iPhone模拟器,而不是iOS设备的工作原理。 为什么? 3个回答

我子类的UIView和使用的那实例设置我的UITableViewCell backgroundView和selectedBackedView性能。 我在我的UIView子类的drawRect方法收到EXC_BAD_ACCESS错误。

    if(nil == cell){

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                 reuseIdentifier:CellIdentifier];
    cell.backgroundView = [[CCViewBackground alloc]init];
    cell.selectedBackgroundView = [[CCViewBackground alloc]init];

    }

UIView子类CCBackgroundView -drawRect:

- (void)drawRect:(CGRect)rect
{
     // Drawing code
     CGContextRef context = UIGraphicsGetCurrentContext();

     CGColorRef redColor = 
     [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0].CGColor;

     CGContextSetFillColorWithColor(context, redColor); //Receiving EXC_BAD_ACCESS here
     CGContextFillRect(context, self.bounds);

}

Answer 1:

我假设你正在使用ARC。 如果是这样,你正在运行到一个众所周知的问题,其中CGColorRef被释放早于你的期望。 这篇文章详细解释了这一问题,并提供了多种解决方案。



文章来源: CGContextRef - EXC_BAD_ACCESS Error [duplicate]