Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

ios - How come my drawing code keeps resulting in fuzzy shapes?

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();

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...