2

I try to modify the Breadcrumb example from Apple in the way that the drawn path has a gradient. I can not make I work. The colour of the path is black and not gradient. Someone got an idea?

if (path != nil)
{

    CGContextAddPath(context, path);
    CGContextSetLineJoin(context, kCGLineJoinRound);
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, lineWidth);

    CGContextReplacePathWithStrokedPath(context);
    path = CGContextCopyPath(context);

    CGContextAddPath(context, path);

    CGContextSaveGState(context); {
        CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
        CGGradientRef gradient = CGGradientCreateWithColors(rgb, (__bridge CFArrayRef)@[
                                                            (__bridge id)[UIColor greenColor].CGColor,
                                                            (__bridge id)[UIColor redColor].CGColor
                                                            ], (CGFloat[]){ 0.0f, 1.0f });
        CGColorSpaceRelease(rgb);

     CGPoint startPoint = CGPointMake(MKMapRectGetMidX(clipRect), MKMapRectGetMinY(clipRect));
     CGPoint endPoint = CGPointMake(MKMapRectGetMidX(clipRect), MKMapRectGetMaxY(clipRect));

        CGContextClip(context);
        CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
        CGGradientRelease(gradient);
    } CGContextRestoreGState(context);

    CGContextAddPath(context, path);
    CGPathRelease(path);

    CGContextDrawPath(context, kCGPathFillStroke);

}
simon
  • 111
  • 1
  • 4
  • You might try calling `CGContextDrawPath` **before** `CGPathRelease`. I think that could cause a problem. – Christian Di Lorenzo Oct 16 '13 at 00:11
  • 1
    I found the soultion myself. The problem are startpoint and endpoint. When you use CGRect bbox = CGContextGetPathBoundingBox(context); CGPoint startPoint = bbox.origin; CGPoint endPoint = CGPointMake(CGRectGetMaxX(bbox),CGRectGetMaxY(bbox)); the colour of the path is gradient. – simon Oct 16 '13 at 07:01

0 Answers0