Mirrored Graphic Image

I draw a circle image using the main graphics with a modified implementation of this SO answer

Here is my source:

+ (UIImage*)circularImageWithRadius:(CGFloat)radius color:(UIColor*)color{
    CGRect rect = CGRectMake(0.0f, 0.0f, radius*2, radius*2);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillEllipseInRect(context, rect);
    UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

The boundaries are blurred, and I'm not sure why (I thought it didn’t matter what resolution the device had, it would work right out of the box).

I tried replacing CGContextFillEllipseInRect(context, rect);with CGContextFillRect(context, rect);, and that too was blurry. Then I tried CGContextFillRect(context, CGRectMake(0, 0, radius*4, radius*4), and it works fine, a sharp image and that's it (although it is a square, not a circle). So I went back to CGContextDrawEllipseInRect(context, CGRectMake(0, 0, radius*4, radius*4), but that was my result:

enter image description here

whereas with a rectangle it was the same size as when using the radius * 2, but with a much sharper image.

CGContextFillEllipseInRect rect?

+3
2

, , .

if(UIGraphicsBeginImageContextWithOptions != NULL) {
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
} else {
    UIGraphicsBeginImageContext(rect.size);
}

UIGraphicsBeginImageContext(rect.size); , .

+9

Swift 3.0

        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
        defer { UIGraphicsEndImageContext() }
        if let context = UIGraphicsGetCurrentContext() {
            /// Do Stuff
        }
0

All Articles