Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone: adding CALayer as Sublayer to UIButton's Layer causes EXC_BAD_ACCESS

hey people, I try to add a nice gradient to a UIButtonTypeRoundedRect. the following code is implemented in the

viewWillAppear:(BOOL)animated   

function of my view controller.

UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
tempButton.frame = CGRectMake(left, top, 80.0f, 80.0f); 

CAGradientLayer * hintergrundButtonLayer = [[CAGradientLayer alloc] init];
[hintergrundButtonLayer setBounds:tempButton.bounds];
[hintergrundButtonLayer setColors:[NSArray arrayWithObjects: 
                   [UIColor colorWithRed:0.2 green:0.3 blue:0.4 alpha:1.0],
                   [UIColor colorWithRed:0.4 green:0.5 blue:0.6 alpha:1.0], 
                   nil]];

[hintergrundButtonLayer setPosition: 
             CGPointMake([tempButton bounds].size.width/2, 
                         [tempButton bounds].size.height/2)];

[hintergrundButtonLayer setLocations:
                       [NSArray arrayWithObjects:
                              [NSNumber numberWithFloat:0.0], 
                              [NSNumber numberWithFloat:1.0], 
                              nil]];

//      hintergrundButtonLayer.zPosition = -10;
//      hintergrundButtonLayer.frame = [tempButton bounds];


[tempButton addTarget:self action:@selector(switchVendorOnOff:) forControlEvents:UIControlEventTouchUpInside];


CALayer * downButtonLayer = [tempButton layer];
[downButtonLayer setMasksToBounds:YES];
[downButtonLayer setCornerRadius:10.0];
[downButtonLayer setBorderWidth:1.2];
[downButtonLayer setBorderColor:[[UIColor blackColor] CGColor]];


[downButtonLayer addSublayer:hintergrundButtonLayer];

[hintergrundButtonLayer release];
hintergrundButtonLayer = nil;

[self.view addSubview:tempButton];

the code runs fine until the app tries to display the view - it crashs with a EXC_BAD_ACCESS. But if I comment out the

[downButtonLayer addSublayer:hintergrundButtonLayer];   

command, then everything is fine.

The debugger tells me nothing about leaks. Also the allocation template shows nothing strange. There is nothing else printed to the console than the EXC_BAD_ACCESS message. The "build and analyse" shows no errors.

Does anyone see my problem?

like image 877
dac Avatar asked Feb 25 '23 03:02

dac


1 Answers

setColors takes CGColors, not UIColors.

[hintergrundButtonLayer setColors:[NSArray arrayWithObjects: 
    [[UIColor colorWithRed:0.2 green:0.3 blue:0.4 alpha:1.0] CGColor],
    [[UIColor colorWithRed:0.4 green:0.5 blue:0.6 alpha:1.0] CGColor], 
    nil]];

If you read the top stack frames in the crash (while running under the debugger), they suggest a problem with CGColors:

(gdb) bt
#0  0x00bd407c in CGColorSpaceGetMD5Digest ()
#1  0x00b4f8b2 in CGColorTransformConvertColorFloatComponents ()
#2  0x00b4fea2 in CGColorTransformConvertColorComponents ()
#3  0x00cb0d62 in CA_CGColorGetRGBComponents ()
#4  0x00d509aa in -[CAGradientLayer _copyRenderLayer:layerFlags:commitFlags:] ()
like image 139
diciu Avatar answered Feb 26 '23 17:02

diciu