Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core graphics images low quality

I'm the new one in Core Graphics and I try to draw circles. They can be transparent (only stroke) and filled with the stroke color. So the method looks like this:

+ (UIImage *)iconImageWithColor: (UIColor *)markColor strokeOnly:(BOOL)strokeOnly
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 20.0f, 20.0f);

    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGPathRef clippingPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:7.5f].CGPath;
    CGContextAddPath(context, clippingPath);
    CGContextClip(context);

    CGContextSetFillColorWithColor(context, strokeOnly == YES ? [[UIColor clearColor] CGColor] : [markColor CGColor]);
    CGContextFillRect(context, rect);

    CGPathRef path = CGPathCreateWithRoundedRect(rect, 10.f, 10.f, NULL);
    [markColor setStroke];
    CGContextAddPath(context, path);
    CGContextDrawPath(context, kCGPathFillStroke);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

The result is strange - it looks like the resolution of the image is very low. Is it possible to make the result image looks good on retina display?

strange image

like image 219
ShurupuS Avatar asked Dec 17 '14 12:12

ShurupuS


2 Answers

UIGraphicsBeginImageContext creates a context with a scale of 1, so non retina. What you want to use is

UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0f)

scale 0 means it will use the device screen scale

like image 70
Fabio Ritrovato Avatar answered Nov 13 '22 03:11

Fabio Ritrovato


Use UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0.0);

Last parameter means the resulting pixel density (scale). When you pass 0 it picks it up automatically from device native screen scale.

like image 35
Rob Zombie Avatar answered Nov 13 '22 05:11

Rob Zombie