我想向我的UIView添加一个阴影,但是在我的drawRect方法中,我得到了一个EXC_BAD_ACCESS。 (我使用ARC)EXC_BAD_ACCES绘制阴影
-(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);
}
错误消息: 线程1:程序接收到的信号: “EXC_BAD_ACCESS”。
线: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);
更新 我终于通过更改来解决问题:
CGColorRef shadowColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.4] .CGColor;
到
浮子部件[4] = {0,0,0,1.0/3.0}; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGColorRef shadowColor = CGColorCreate(colorSpace,components);
最终的(工作)代码:
-(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);
}
在这里发布确切的错误信息。 –
错误消息不会真的有帮助,但为了您的舒适(请参阅编辑)。 – Thys
你的调试器应该至少告诉你哪一行崩溃了。 – DarkDust