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?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With