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

dev*_*os1 1 uitableview ios automatic-ref-counting cagradientlayer

在我的表格视图控制器的以下代码中,如果我使用“库存”颜色,[UIColor blueColor]它可以正常工作,但是如果我尝试使用 RGBA 值创建自定义颜色,它会失败并且除了纯黑色之外不绘制任何东西。

这是因为 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];
}
Run Code Online (Sandbox Code Playgroud)

Ism*_*ael 5

问题是,当你这样做时,96/255它会将值解释为 int,结果为 0

尝试96.0/255.0在创建 UIColor 时编写值