Ios 尝试使用alpha设置颜色崩溃

Ios 尝试使用alpha设置颜色崩溃,ios,core-graphics,exc-bad-access,alpha,uicolor,Ios,Core Graphics,Exc Bad Access,Alpha,Uicolor,我正在尝试将填充UIColor设置为具有alpha glassColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.3].CGColor; CGContextSetFillColorWithColor(context, glassColor); ... 第二行因EXC\u BAD\u访问失败而崩溃 想知道为什么吗?在ARC中尝试使用 UIColor *glasscolor = [UIColor colorWithR

我正在尝试将填充
UIColor
设置为具有
alpha

glassColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.3].CGColor;
CGContextSetFillColorWithColor(context, glassColor);
...
第二行因
EXC\u BAD\u访问失败而崩溃

想知道为什么吗?

在ARC中尝试使用

       UIColor *glasscolor = [UIColor colorWithRed: red 
                                   green: green 
                                    blue: blue 
                                   alpha: your alpha value];// 
       [view setBackgroundColor:glasscolor];
非常浅灰色的示例

        UIColor *glasscolor = [UIColor colorWithRed:0.945 green:0.945 blue:0.945 alpha:1.0]
        [view setBackgroundColor:glasscolor]; 
在弧中尝试使用

       UIColor *glasscolor = [UIColor colorWithRed: red 
                                   green: green 
                                    blue: blue 
                                   alpha: your alpha value];// 
       [view setBackgroundColor:glasscolor];
非常浅灰色的示例

        UIColor *glasscolor = [UIColor colorWithRed:0.945 green:0.945 blue:0.945 alpha:1.0]
        [view setBackgroundColor:glasscolor]; 

本质上,您的问题是ARC正在释放UIColor,因为它没有被使用。您需要保留CGColor结构。你可以用下面的方法来做

 CGColorRef aColorRef = CGColorRetain([[UIColor colorWithWhite:0.82 alpha:1.0] CGColor] );
 CGContextSetFillColorWithColor(context, aColorRef);
使用完毕后,别忘了发布:

 CGColorRelease(aColorRef); 

本质上,您的问题是ARC正在释放UIColor,因为它没有被使用。您需要保留CGColor结构。你可以用下面的方法来做

 CGColorRef aColorRef = CGColorRetain([[UIColor colorWithWhite:0.82 alpha:1.0] CGColor] );
 CGContextSetFillColorWithColor(context, aColorRef);
使用完毕后,别忘了发布:

 CGColorRelease(aColorRef);