I have a gradient that has rounded corners that I an using for a custom UITableViewCell background. I am trying to apply a stroke to the path but cannot quite do it, and can't see where I am going wrong.
CGFloat minx = CGRectGetMinX(rect) , midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect) ;
CGFloat miny = CGRectGetMinY(rect) , maxy = CGRectGetMaxY(rect) ;
minx = minx + 1;
miny = miny ;
maxx = maxx - 1;
maxy = maxy - 1;
CGContextMoveToPoint(c, minx, miny);
CGContextAddArcToPoint(c, minx, maxy, midx, maxy, kDefaultMargin);
CGContextAddArcToPoint(c, maxx, maxy, maxx, miny, kDefaultMargin);
CGContextAddLineToPoint(c, maxx, miny);
CGContextAddLineToPoint(c, minx, miny);
// Fill and stroke the path
CGContextClip(c);
CGContextStrokePath(c);
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat mycomponents[8] = TABLE_CELL_BACKGROUND;
CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef myGradient = CGGradientCreateWithColorComponents(myColorspace, mycomponents, locations, 2);
CGContextDrawLinearGradient(c, myGradient, CGPointMake(minx,miny), CGPointMake(minx,maxy), 0);
CGGradientRelease(myGradient);
CGColorSpaceRelease(myColorspace);
This code rounds the rectangle right and applies the gradient, but does not stroke the row. Where is my mistake?
EDIT:
As per subw's recommendations, I changed the code to be:
CGContextRef c = UIGraphicsGetCurrentContext();
CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef myGradient = nil;
CGFloat components[8] = TABLE_CELL_BACKGROUND;
CGContextSetFillColorWithColor(c, [[UIColor redColor] CGColor]);
CGContextSetStrokeColorWithColor(c, [[UAColor redColor] CGColor]);
CGContextSetLineWidth(c, 2);
CGFloat minx = CGRectGetMinX(rect) , midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect) ;
CGFloat miny = CGRectGetMinY(rect) , maxy = CGRectGetMaxY(rect) ;
minx = minx + 1;
miny = miny ;
maxx = maxx - 1;
maxy = maxy - 1;
CGContextMoveToPoint(c, minx, miny);
CGContextAddArcToPoint(c, minx, maxy, midx, maxy, kDefaultMargin);
CGContextAddArcToPoint(c, maxx, maxy, maxx, miny, kDefaultMargin);
CGContextAddLineToPoint(c, maxx, miny);
CGContextAddLineToPoint(c, minx, miny);
// Fill and stroke the path
CGContextSaveGState(c);
CGContextClip(c);
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat mycomponents[8] = TABLE_CELL_BACKGROUND;
CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB();
myGradient = CGGradientCreateWithColorComponents(myColorspace, mycomponents, locations, 2);
CGContextDrawLinearGradient(c, myGradient, CGPointMake(minx,miny), CGPointMake(minx,maxy), 0);
CGContextRestoreGState(c);
CGContextStrokePath(c);
But the rect comes out like this…
...with no border. If I change the CGContextStrokePath(c)
to be CGContextStrokeRect(c, rect)
, the stroke shows up (I made it red for clarification). But somehow, when stroking the path, I am not getting any stroke :(