Detect Retina Display
Solution 1:
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
}
Solution 2:
@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..
}
Solution 3:
+(BOOL)iPhoneRetina{
return ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && ([UIScreen mainScreen].scale == 2.0))?1:0;
}
Solution 4:
Here is a handy swift extension:
Update for Swift v5:
extension UIScreen {
public var isRetina: Bool {
guard let scale = screenScale else {
return false
}
return scale >= 2.0
}
public var isRetinaHD: Bool {
guard let scale = screenScale else {
return false
}
return scale >= 3.0
}
private var screenScale: CGFloat? {
guard UIScreen.main.responds(to: #selector(getter: scale)) else {
return nil
}
return UIScreen.main.scale
}
}
Usage:
if UIScreen.main.isRetina {
// Your code
}
Original:
extension UIScreen {
public func isRetina() -> Bool {
return screenScale() >= 2.0
}
public func isRetinaHD() -> Bool {
return screenScale() >= 3.0
}
private func screenScale() -> CGFloat? {
if UIScreen.mainScreen().respondsToSelector(Selector("scale")) {
return UIScreen.mainScreen().scale
}
return nil
}
}
Usage:
if UIScreen.mainScreen().isRetina() {
// your code
}