Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use Core Graphics to draw an arc with a gradient fill, a stroked outline, and a shadow. http://stackoverflow.com/questions/15866179/draw-segments-from-a-circle-or-donut
static UIImage *imageWithSize(CGSize size) {
static CGFloat const kThickness = 20;
static CGFloat const kLineWidth = 1;
static CGFloat const kShadowWidth = 8;
UIGraphicsBeginImageContextWithOptions(size, NO, 0); {
CGContextRef gc = UIGraphicsGetCurrentContext();
CGContextAddArc(gc, size.width / 2, size.height / 2,
(size.width - kThickness - kLineWidth) / 2,
-M_PI / 4, -3 * M_PI / 4, YES);
CGContextSetLineWidth(gc, kThickness);
CGContextSetLineCap(gc, kCGLineCapButt);
CGContextReplacePathWithStrokedPath(gc);
CGPathRef path = CGContextCopyPath(gc);
CGContextSetShadowWithColor(gc,
CGSizeMake(0, kShadowWidth / 2), kShadowWidth / 2,
[UIColor colorWithWhite:0 alpha:0.3].CGColor);
CGContextBeginTransparencyLayer(gc, 0); {
CGContextSaveGState(gc); {
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColors(rgb, (__bridge CFArrayRef)@[
(__bridge id)[UIColor grayColor].CGColor,
(__bridge id)[UIColor whiteColor].CGColor
], (CGFloat[]){ 0.0f, 1.0f });
CGColorSpaceRelease(rgb);
CGRect bbox = CGContextGetPathBoundingBox(gc);
CGPoint start = bbox.origin;
CGPoint end = CGPointMake(CGRectGetMaxX(bbox), CGRectGetMaxY(bbox));
if (bbox.size.width > bbox.size.height) {
end.y = start.y;
} else {
end.x = start.x;
}
CGContextClip(gc);
CGContextDrawLinearGradient(gc, gradient, start, end, 0);
CGGradientRelease(gradient);
} CGContextRestoreGState(gc);
CGContextAddPath(gc, path);
CGPathRelease(path);
CGContextSetLineWidth(gc, kLineWidth);
CGContextSetLineJoin(gc, kCGLineJoinMiter);
[[UIColor blackColor] setStroke];
CGContextStrokePath(gc);
} CGContextEndTransparencyLayer(gc);
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.