CGContextRef - EXC_BAD_ACCESS错误 [英] CGContextRef - EXC_BAD_ACCESS Error

查看:162
本文介绍了CGContextRef - EXC_BAD_ACCESS错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是子类化UIView并使用它的实例来设置我的UITableViewCell backgroundView和selectedBackedView属性。我在我的UIView子类的drawRect方法中收到一个EXC_BAD_ACCESS错误。

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子类CCBackgroundView -drawRect:

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

}


推荐答案

你使用ARC。如果是这样,你遇到了一个众所周知的问题,其中 CGColorRef 发布比你预期的早。 本文详细解释了该问题,并提供了几种解决方案。

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.

这篇关于CGContextRef - EXC_BAD_ACCESS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆