核心图形圆形图像模糊 [英] Core Graphics Circular Image Blurry
问题描述
我正在使用核心图形绘制圆形图像,并对此指明了方向。
我刚添加
if(UIGraphicsBeginImageC ontextWithOptions!= NULL){
UIGraphicsBeginImageContextWithOptions(rect.size,NO,0.0);
} else {
UIGraphicsBeginImageContext(rect.size);
}
而不是 UIGraphicsBeginImageContext(rect.size);
进入我的原始资源,它清晰而清晰。
I'm drawing a circle image using core graphics with a modified implementation of this SO answer
Here's 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 edges are blurry and I'm not sure why (I thought it didn't matter what resolution the device was, it would work right out of the box).
I tried replacing CGContextFillEllipseInRect(context, rect);
with CGContextFillRect(context, rect);
and that was blurry too. Then I tried CGContextFillRect(context, CGRectMake(0, 0, radius*4, radius*4)
and it works perfectly, sharp image and everything (albeit being a a square, not a circle). So I changed back to CGContextDrawEllipseInRect(context, CGRectMake(0, 0, radius*4, radius*4)
but this was my result:
whereas with the rectangle, it was the same size as when using radius*2 but with a much sharper image.
How can I fix my blurry issue and why does CGContextFillEllipseInRect
not fill the pre-defined image rect?
I feel dumb for having found this immediately after I posted but this answer pointed the way.
I just added
if(UIGraphicsBeginImageContextWithOptions != NULL) {
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
} else {
UIGraphicsBeginImageContext(rect.size);
}
instead of just UIGraphicsBeginImageContext(rect.size);
into my original source and it's crystal clear and sharp.
这篇关于核心图形圆形图像模糊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!