CGContextStrokePath触发iOS中的EXC_BAD_ACCESS>五

时间:2012-06-27 15:08:52

标签: objective-c ios5 ios4 exc-bad-access quartz-2d

我有一个自定义UIButton类,它为UIButton添加了渐变和光泽效果 代码在iOS 4和iOS5模拟器上运行完美,但是当我在iOS 5设备上运行它时,它给我异常EXC_BAD_ACCESS,异常由该行触发:

CGContextStrokePath(context);

任何帮助都非常感谢, 这是我的代码

- (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);

}

我现在真正想要的是它真的与iOS 5有关,还是我做错了什么?

1 个答案:

答案 0 :(得分:4)

发生崩溃是因为在访问CGColorRefs时已经释放了UIColors。

避免这种情况的一种简单方法是使用

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

而不是

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

所以ARC没有机会尽早解除分配UIColor对象。