Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9041015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:08:03+00:00 2026-06-16T10:08:03+00:00

I have a custom UIButton class that adds a gradient and a gloss effect

  • 0

I have a custom UIButton class that adds a gradient and a gloss effect to the UIButton
the code works perfectly in iOS 4 and on iOS5 simulator but when i run it on iOS 5 devices it gives me the exception EXC_BAD_ACCESS , the exception is triggered by the line :

CGContextStrokePath(context);

any help is really appreciated ,
here’s my code

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGFloat actualBrightness = _brightness;
    if (self.selected) {
        actualBrightness -= 0.10;
    }   

    CGColorRef blackColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0].CGColor;
    CGColorRef highlightStart = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.7].CGColor;
    CGColorRef highlightStop = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0].CGColor;

    CGColorRef outerTop = [UIColor colorWithHue:_hue saturation:_saturation brightness:1.0*actualBrightness alpha:1.0].CGColor;
    CGColorRef outerBottom = [UIColor colorWithHue:_hue saturation:_saturation brightness:0.80*actualBrightness alpha:1.0].CGColor;

    CGFloat outerMargin = 7.5f;
    CGRect outerRect = CGRectInset(self.bounds, outerMargin, outerMargin);            
    CGMutablePathRef outerPath = createRoundedRectForRect(outerRect, 6.0);

    // Draw gradient for outer path
    CGContextSaveGState(context);
    CGContextAddPath(context, outerPath);
    CGContextClip(context);
    drawLinearGradient(context, outerRect, outerTop, outerBottom);

    CGContextRestoreGState(context);

    if (!self.selected) {

        CGRect highlightRect = CGRectInset(outerRect, 1.0f, 1.0f);
        CGMutablePathRef highlightPath = createRoundedRectForRect(highlightRect, 6.0);

        CGContextSaveGState(context);
        CGContextAddPath(context, outerPath);
        CGContextAddPath(context, highlightPath);
        CGContextEOClip(context);

        drawLinearGradient(context, CGRectMake(outerRect.origin.x, outerRect.origin.y, outerRect.size.width, outerRect.size.height/3), highlightStart, highlightStop);
        CGContextRestoreGState(context);

        drawCurvedGloss(context, outerRect, 180);
        CFRelease(highlightPath);

    }
    else {
        //reverse non-curved gradient when pressed
        CGContextSaveGState(context);
        CGContextAddPath(context, outerPath);
        CGContextClip(context);
        drawLinearGloss(context, outerRect, TRUE);      
        CGContextRestoreGState(context);

    }
    if (!_toggled) {
        //bottom highlight
        CGRect highlightRect2 = CGRectInset(self.bounds, 6.5f, 6.5f);
        CGMutablePathRef highlightPath2 = createRoundedRectForRect(highlightRect2, 6.0);

        CGContextSaveGState(context);
        CGContextSetLineWidth(context, 0.5);
        CGContextAddPath(context, highlightPath2);
        CGContextAddPath(context, outerPath);
        CGContextEOClip(context);
        drawLinearGradient(context, CGRectMake(self.bounds.origin.x, self.bounds.size.height-self.bounds.size.height/3, self.bounds.size.width, self.bounds.size.height/3), highlightStop, highlightStart);

        CGContextRestoreGState(context);
        CFRelease(highlightPath2);
    }
    else {
        //toggle marker
        CGRect toggleRect= CGRectInset(self.bounds, 5.0f, 5.0f);
        CGMutablePathRef togglePath= createRoundedRectForRect(toggleRect, 8.0);

        CGContextSaveGState(context);
        CGContextSetLineWidth(context, 3.5);
        CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
        CGContextAddPath(context, togglePath);
        CGContextStrokePath(context);
        CGContextRestoreGState(context);
        CFRelease(togglePath);
    }
    // Stroke outer path
    CGContextSaveGState(context);
    CGContextSetLineWidth(context, 0.5);
    CGContextSetStrokeColorWithColor(context, blackColor);
    CGContextAddPath(context, outerPath);
    CGContextStrokePath(context);
    CGContextRestoreGState(context);

    CFRelease(outerPath);

}

what I really wanna now is that is it really related to iOS 5 or am i doing something else wrong ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T10:08:05+00:00Added an answer on June 16, 2026 at 10:08 am

    The crash happens because the UIColors are already deallocated when you access the CGColorRefs.

    An easy way to avoid this is to use

    UIColor* blackColor = [UIColor blackColor];
    CGContextSetStrokeColorWithColor(context, [blackColor CGColor]);
    

    instead of

    CGColorRef* blackColor = [[UIColor blackColor] CGColor];
    CGContextSetStrokeColorWithColor(context, blackColor);
    

    so ARC doesn’t get the chance to deallocate the UIColor objects early.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom class, and that class has a UIButton instance variable. I
In my iPad App, I have one custom class inherited from UIButton. I am
I have a very simply subclass of UIButton that will fire off a custom
I have a custom button, subclassed from UIButton that is initialised as below (obviously
So I have made a custom UIButton and added it to the code and
I have a custom UIButton in my viewcontroller that should have either the image
I have a custom button that is a subclassed UIButton . When I try
I have a custom UIButton that has the label Name. When the user clicks
I have a custom UIButton class in my IPhone navigation application. When the user
I have a custom UIbutton in every cell of my tableview. This UIbutton works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.