【问题标题】:Core graphics images low quality核心图形图像质量低
【发布时间】:2014-12-17 12:37:18
【问题描述】:

我是 Core Graphics 的新人,我尝试画圆。它们可以是透明的(仅描边)并用描边颜色填充。所以方法看起来是这样的:

+ (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;
}

结果很奇怪——看起来图像的分辨率很低。是否可以使结果图像在视网膜显示器上看起来不错?

【问题讨论】:

  • 在绘制矩形之前添加这条线:CGContextSetLineWidth(context, 1.0);您可以根据您的要求将 1.0 更新为。它的画线宽度。

标签: ios objective-c core-graphics


【解决方案1】:

UIGraphicsBeginImageContext 创建一个比例为 1 的上下文,因此非视网膜。 你要使用的是

UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0f)

scale 0 表示将使用设备屏幕比例

【讨论】:

  • 难以置信,你是我的掌上明珠
【解决方案2】:

使用UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0.0);

最后一个参数表示生成的像素密度(比例)。当您传递 0 时,它会自动从设备本机屏幕比例中拾取它。

【讨论】:

    猜你喜欢
    • 2011-07-21
    • 1970-01-01
    • 2014-10-14
    • 2019-08-23
    • 2021-06-16
    • 2014-12-24
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多