为什么我的绘图代码总是导致形状模糊? [英] How come my drawing code keeps resulting in fuzzy shapes?

查看:19
本文介绍了为什么我的绘图代码总是导致形状模糊?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论我做什么,我似乎都无法弄清楚为什么我的图像在 iOS 显示器上总是模糊不清.无论是我在图像编辑器中创建的还是在绘图代码中创建的都没有关系,但我认为该代码会使问题更容易解析.我知道如果线宽 1 像素,它会显得模糊,但我尝试了多种线宽尺寸,结果相似.我该怎么做才能确保我的图形看起来很清晰?

No matter what I do, I can't seem to figure out why my images keep coming out fuzzy on iOS displays. It doesn't matter whether it's one I created in an image editor or drawing code, but I figured that code would make the issue much easier to parse. I know that if the line is 1 pixel wide, it'll appear fuzzy, but I tried multiple line width sizes with similar results. What can I do to make sure my graphics look sharp?

UIGraphicsBeginImageContext(CGSizeMake(1000, 1000));
    UIBezierPath *bezier = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 1000, 1000)];
    [[UIColor greenColor]set];
    [bezier fill];
    [[UIColor blackColor]set];
    bezier = [UIBezierPath bezierPath];
    [bezier setLineWidth:1.5]; //I tried .5, 1, 1.5, & 2 with similar results
    [bezier moveToPoint:CGPointMake(450, 500)];
    [bezier addLineToPoint:CGPointMake(500, 400)];
    [bezier addLineToPoint:CGPointMake(550, 500)];
    [bezier closePath];
    [bezier stroke];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

推荐答案

UIGraphicsBeginImageContext:

... 相当于调用UIGraphicsBeginImageContextWithOptions 函数与不透明参数设置为 NO,比例因子为 1.0.

... is equivalent to calling the UIGraphicsBeginImageContextWithOptions function with the opaque parameter set to NO and a scale factor of 1.0.

然而,UIGraphicsBeginImageContextWithOptions 将它的 scale 参数定义为:

However, UIGraphicsBeginImageContextWithOptions defines its scale parameter as:

规模
应用于位图的比例因子.如果您指定值 0.0,则比例因子设置为设备主屏幕的比例因子.

scale
The scale factor to apply to the bitmap. If you specify a value of 0.0, the scale factor is set to the scale factor of the device’s main screen.

因此,您对 UIGraphicsBeginImageContextWithOptions 的使用正在创建一个 1.0 规模的上下文.这是正确的,例如去年的 iPad Mini,但所有视网膜级设备都有 2.0 的原生"比例——每个逻辑点两个像素.UIGraphicsBeginImageContextWithOptions 引入了 0.0 的特殊情况,这样您就不必为自己获取该设备的正确编号而烦恼(尽管 [[UIScreenmainScreen] scale] 将是一个简单的方法).

So your use of UIGraphicsBeginImageContextWithOptions is creating a context with a scale of 1.0. That's correct for e.g. last year's iPad Mini but all retina-class devices have a 'native' scale of 2.0 — two pixels per logical point. UIGraphicsBeginImageContextWithOptions introduced the special case of 0.0 so that you don't have to mess about dealing with getting the correct number for that device for yourself (though [[UIScreen mainScreen] scale] would be an easy way).

因此将退出调用切换到 UIGraphicsBeginImageContext(CGSizeMake(1000, 1000)) 到:

So switch your exiting call to UIGraphicsBeginImageContext(CGSizeMake(1000, 1000)) to:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(1000, 1000), NO, 0.0)

否则,您创建的上下文在相应空间中的像素数将是屏幕的四分之一.

Otherwise the context you create will have a quarter as many pixels as the screen does in the corresponding amount of space.

这篇关于为什么我的绘图代码总是导致形状模糊?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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