Mattia Lancieri Mattia Lancieri - 1 year ago 223
iOS Question

Draw a simple circle uiimage

I try to make a 20x20 UIImage with a simple blue circle.
I try with this function, but the result is a blue circle in a black square.
how do I remove the black square around the circle?


+ (UIImage *)blueCircle {
static UIImage *blueCircle = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(20.f, 20.f), YES, 0.0f);
CGContextRef ctx = UIGraphicsGetCurrentContext();

CGRect rect = CGRectMake(0, 0, 20, 20);
CGContextSetFillColorWithColor(ctx, [UIColor cyanColor].CGColor);
CGContextFillEllipseInRect(ctx, rect);

blueCircle = UIGraphicsGetImageFromCurrentImageContext();

return blueCircle;

actual result:
enter image description here

Answer Source

You need to include alpha channel into your bitmap: UIGraphicsBeginImageContextWithOptions(..., NO, ...) if you want to see what is behind the corners.