问题
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();
回答1:
UIGraphicsBeginImageContext:
... is equivalent to calling the
UIGraphicsBeginImageContextWithOptionsfunction with the opaque parameter set toNOand a scale factor of1.0.
However, UIGraphicsBeginImageContextWithOptions defines its scale parameter as:
scale
The scale factor to apply to the bitmap. If you specify a value of0.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.
来源:https://stackoverflow.com/questions/25210620/how-come-my-drawing-code-keeps-resulting-in-fuzzy-shapes