Low-quality basic graphics

I am new to Core Graphics and I try to draw circles. They can be transparent (only strokes) and filled with the stroke color. Thus, the method is as follows:

+ (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 seems that the image resolution is very low. Is it possible for the result image to look good on the retina display?

strange image

+6
source share
2 answers

UIGraphicsBeginImageContext creates a context with a scale of 1, so not the retina. Do you want to use

UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0f)

a scale of 0 means that it will use the device’s screen scale

+10
source

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

The last parameter means the resulting pixel density (scale). When you pass 0, it automatically selects it from the device’s own screen scale.

+4
source

Source: https://habr.com/ru/post/979781/


All Articles