UIGraphicsBeginImageContext must be Retina-aware?
Asked Answered
C

2

20

I've read some posts which recommend the use of:

UIGraphicsBeginImageContextWithOptions((image.size), NO, 0.0f) 

instead of:

UIGraphicsBeginImageContext(image.size).

Because the first one uses Retina-scale when drawing, otherwise you get blocky images.

I'm a little confused about when to use or not the retina-scale option.

If i have a photograph which is 1000x1000 px, and i call UIGraphicsBeginImageContext passing those dimensions, am i not specifying exactly how many points i want to draw? Is that ok?

Or should i call UIGraphicsBeginImageContextWithOptions passing half the dimensions (500x500px)? Does it make any difference?

Cooperman answered 22/7, 2012 at 19:9 Comment(1)
I've read some posts which recommend the use of... Because the first one uses Retina-scale when drawing, otherwise you get blocky images. - this note helped me a lot. Thanks!Withindoors
E
15

You can create the image context like this:

 UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
Egression answered 15/9, 2015 at 20:30 Comment(2)
According to the docs: “If you specify a value of 0.0, the scale factor is set to the scale factor of the device’s main screen.”. Thus, using 0.0 there—instead of UIScreen.mainScreen().scale—has the same effect, but will benefit from under-the-hood optimizations.Bloodyminded
Thank you for this info, I see the docs say that scale = 0.0 equals the current screen scale, but cannot find anything about under the hood optimizations. Can you elaborate ?Egression
G
12

You can get the scale value of your device by the following method

[[UIScreen mainScreen] scale]

With this information you can decide how many points you would like to draw.

Glabrescent answered 22/7, 2012 at 19:23 Comment(3)
I know, but what i'd like to now is if using: UIGraphicsBeginImageContext(1000x1000px) is any different from using UIGraphicsBeginImageContextWithOptions((500x500px), NO, 0.0f).Cooperman
isn't retina only important when showing the image, not drawing? i mean, i think it doesn't matter the size of the image, as long as when i'm showing it, i display it in a view with half the size (retina).Cooperman
@Cooperman They both give you a 1000x1000px image. But I think the different is when using UIGraphicsBeginImageContext(1000x1000px), the drawing are display on the screen is with 1000x1000 points, but UIGraphicsBeginImageContextWithOptions((500x500px), NO, 0.0f) with 500x500 points, that's my guess.Glabrescent

© 2022 - 2024 — McMap. All rights reserved.