首页 文章

EXC_BAD_ACCES绘制阴影

提问于
浏览
3

我正在尝试为我的UIView添加阴影,但在我的drawRect方法中,我得到了一个EXC_BAD_ACCESS . (我正在使用ARC)

-(void) drawRect:(CGRect)rect {

    CGColorRef lightColor =  [UIColor colorWithRed:105.0f/255.0f green:179.0f/255.0f blue:216.0f/255.0f alpha:0.8].CGColor;

    CGColorRef shadowColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.4].CGColor;   

    CGContextRef context = UIGraphicsGetCurrentContext();
    // Draw shadow
    CGContextSaveGState(context);
    CGContextSetShadowWithColor(context, CGSizeMake(-5, 0), 10, shadowColor);
    CGContextSetFillColorWithColor(context, lightColor);
    CGContextFillRect(context, _coloredBoxRect);
    CGContextRestoreGState(context);
}

Error Message: 线程1:编程接收信号:"EXC_BAD_ACCESS" .

Line: CGContextSetFillColorWithColor(context, lightColor);

当我将此行更改为:

[[UIColor colorWithRed:105.0f/255.0f green:179.0f/255.0f blue:216.0f/255.0f alpha:0.8] setFill];

我得到了相同的错误,但在这一行:

CGContextSetShadowWithColor(context, CGSizeMake(-5, 0), 10, shadowColor);

Update 我最后通过改变来解决这个问题:

CGColorRef shadowColor = [UIColor colorWithRed:0.2绿色:0.2蓝色:0.2 alpha:0.4] .CGColor;

float components [4] = {0,0,0,1.0 / 3.0}; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGColorRef shadowColor = CGColorCreate(colorSpace,components);

The eventual (working) code:

-(void) drawRect:(CGRect)rect 
{
    float components[4] = {0, 0, 0, 1.0/3.0};
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef shadowColor = CGColorCreate( colorSpace, components);

    CGContextRef context = UIGraphicsGetCurrentContext();

    // Draw shadow
    CGContextSaveGState(context);
    CGContextSetShadowWithColor(context, CGSizeMake(-5, 0), 10, shadowColor);
    CGContextSetFillColorWithColor(context, lightColor);

    [[UIColor colorWithRed:105.0f/255.0f green:179.0f/255.0f blue:216.0f/255.0f alpha:0.8] setFill];

    CGContextRestoreGState(context);
}

4 回答

  • 5

    我看不出有什么原因会崩溃,但尝试使用此代码设置颜色而不是使用CGColorRef . 如果它没有解决崩溃至少你会知道该行不是问题:

    [[UIColor colorWithRed:105.0f/255.0f green:179.0f/255.0f blue:216.0f/255.0f alpha:0.8] setFill];
    
  • 0

    启用ARC后, UIColor 可能无法进入自动释放池 . 如果它没有放入池中并立即释放,那么你引用的 CGColor (lightColor,shadowColor)也会在你传递它时被解除分配,因为它们由_3025119持有/拥有,并且没有采取任何措施确保这些(非 NSObject )引用在该范围之外仍然有效 .

    我无法重现您的确切问题,但我可以使用以下方法重现它:

    CGColorRef shadowColor =
      [[UIColor alloc] initWithRed:0.2 green:0.2 blue:0.2 alpha:0.4].CGColor;
    

    在sim v5.0上运行时 .

    你发布了确切的例子吗?您运行的操作系统版本是什么?是否会在所有操作系统版本中发生?也许你应该看看asm .

  • 2

    或者,您可以告诉编译器将 UIColor 对象添加到自动释放池中,而不是立即释放它们 .

    UIColor * __autoreleasing lightUIColor = [UIColor colorWithRed:105.0f/255.0f green:179.0f/255.0f blue:216.0f/255.0f alpha:0.8];
    CGColorRef lightColor =  lightUIColor.CGColor;
    
    UIColor * __autoreleasing shadowUIColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.4];
    CGColorRef shadowColor = shadowUIColor.CGColor;
    
  • 3

    以下代码将满足ARC:

    UIColor *lightColor =  [UIColor colorWithRed:105.0f/255.0f green:179.0f/255.0f blue:216.0f/255.0f alpha:0.8];
    ...
    CGContextSetFillColorWithColor(context, lightColor.CGColor);
    

    在您创建临时UIColor *对象后立即解除分配的原因 .

相关问题