核心图形圆形图像模糊 [英] Core Graphics Circular Image Blurry

查看:85
本文介绍了核心图形圆形图像模糊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用核心图形绘制圆形图像,并对此指明了方向。



我刚添加

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆