CGContextRef - EXC_BAD_ACCESS Error [duplicate]

情到浓时终转凉″ 提交于 2019-12-12 17:31:25

问题


I am subclassing UIView and using instances of that to set my UITableViewCell backgroundView and selectedBackedView properties. I am receiving an EXC_BAD_ACCESS error in the drawRect method of my UIView subclass.

    if(nil == cell){

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

    }

UIView subclass 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);

}

回答1:


I assume you're using ARC. If so, you are running into a well-known problem where the CGColorRef is released earlier than you expect. This article explains the problem in detail and provides several solutions.



来源:https://stackoverflow.com/questions/12237554/cgcontextref-exc-bad-access-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!