我的绘图代码如何保持模糊形状? [英] How come my drawing code keeps resulting in fuzzy shapes?

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

问题描述

无论我做什么,我似乎都无法弄清楚为什么我的图像在iOS显示屏上显得模糊不清。
它是否是我在图像编辑器或绘图代码中创建的无关紧要,但我认为代码会使问题更容易解析。我知道,如果这条线是1像素宽,它会显得模糊,但我尝试了多个线宽大小,结果类似。我可以做些什么来确保我的图形看起来很清晰?

  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]; //我试过.5,1,1.5,& 2结果相似
[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


但是, UIGraphicsBeginImageContextWithOptions 将其< scale 参数定义为:
$ b


缩放比例
应用于位图的比例因子。如果您指定 0.0 的值,则比例因子将设置为设备主屏幕的比例因子。


因此,使用 UIGraphicsBeginImageContextWithOptions 会创建一个尺度为 1.0 的上下文。这对于例如去年的iPad Mini,但所有视网膜级设备的原生比例为 2.0 - 每个逻辑点两个像素。 UIGraphicsBeginImageContextWithOptions 介绍了 0.0 的特殊情况,以便您不必处理获取正确的数字(尽管 [[UIScreen mainScreen] scale] 会是一个简单的方法)。因此,切换您退出对 UIGraphicsBeginImageContext(CGSizeMake(1000,1000))的调用:

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

有相当于屏幕在相应的空间数量的四分之一像素。


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:

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

However, UIGraphicsBeginImageContextWithOptions defines its scale parameter as:

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.

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).

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天全站免登陆