Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Retina Display

Does iOS SDK provides an easy way to check if the currentDevice has an high-resolution display (retina) ?

The best way I've found to do it now is :

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] == YES && [[UIScreen mainScreen] scale] == 2.00) {
         // RETINA DISPLAY
    }
like image 426
Pierre Valade Avatar asked Oct 09 '22 20:10

Pierre Valade


2 Answers

In order to detect the Retina display reliably on all iOS devices, you need to check if the device is running iOS4+ and if the [UIScreen mainScreen].scale property is equal to 2.0. You CANNOT assume a device is running iOS4+ if the scale property exists, as the iPad 3.2 also contains this property.

On an iPad running iOS3.2, scale will return 1.0 in 1x mode, and 2.0 in 2x mode -- even though we know that device does not contain a Retina display. Apple changed this behavior in iOS4.2 for the iPad: it returns 1.0 in both 1x and 2x modes. You can test this yourself in the simulator.

I test for the -displayLinkWithTarget:selector: method on the main screen which exists in iOS4.x but not iOS3.2, and then check the screen's scale:

if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] &&
    ([UIScreen mainScreen].scale == 2.0)) {
  // Retina display
} else {
  // non-Retina display
}
like image 296
Trashpanda Avatar answered Oct 16 '22 14:10

Trashpanda


@sickp's answer is correct. Just to make things easier, add this line into your Shared.pch file:

#define IS_RETINA ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && ([UIScreen mainScreen].scale >= 2.0))

Then in any file you can just do:

if(IS_RETINA)
{
   // etc..
}
like image 81
Mick Byrne Avatar answered Oct 16 '22 12:10

Mick Byrne