为什么我不能在ARC中使用自定义颜色创建CAGradientLayer?

问题描述:

在我的表格视图控制器的以下代码中,如果我使用像[UIColor blueColor]这样的“库存”颜色,它可以正常工作,但是如果我尝试使用RGBA值创建自定义颜色,它会失败并且不会绘制任何物体, 。为什么我不能在ARC中使用自定义颜色创建CAGradientLayer?

这是因为ARC在它可以被使用之前释放CGColorRef吗?我如何才能使用自定义颜色工作?

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    [cell setBackgroundColor:[UIColor clearColor]]; 

    CAGradientLayer *selectedGrad = [CAGradientLayer layer]; 
    selectedGrad.frame = cell.bounds; 

    UIColor* colorOne = [UIColor blueColor];//[UIColor colorWithRed:96/255 green:157/255 blue:227/255 alpha:1]; 
    UIColor* colorTwo = [UIColor greenColor];//[UIColor colorWithRed:54/255 green:123/255 blue:201/255 alpha:1]; 

    NSArray* colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, colorTwo.CGColor, nil]; 

    selectedGrad.colors = colors; 
    selectedGrad.locations = [NSArray arrayWithObjects:@(0.0), @(1.0), nil]; 

    [cell setSelectedBackgroundView:[[UIView alloc] init]]; 
    [cell.selectedBackgroundView.layer insertSublayer:selectedGrad atIndex:0]; 
} 
+0

就像一个健全的检查,确保'cell.selectedBackgroundView'设置后不是零。 – Ariel

的问题是,当你做96/255它解释值如int,导致0

试着写的数值为96.0/255.0创建的UIColor

+0

Aggh。我认为编译器会更聪明。 :/太习惯于生活在JavaScript/C#我猜。 – devios1

时,这是一个附加的正确方法RGB颜色:

[UIColor colorWithRed:0x63/255.0 green:0x4E/255.0 blue:0x42/255.0 alpha:1]