Search code examples
iosiphoneobjective-cuiimageretina-display

How to deal with Iphone 5 retina display check?


When running app in Iphone 5, I discovered that [UIImage imageNamed] doesn't detect retina display when it comes to iphone or ipod 5. I have 2 images for everything in my app, the standard one, and the retina one named @2x. Now, I used to select the regular image in storyboard or programmatically because I thought it would get the right image automatically, like simulator does, but turns out, Iphone 5 doesn't. So, for my understanding, I have to manually check if is retina using the code:

if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] &&
        ([UIScreen mainScreen].scale == 2.0)) {
        // Retina display
        NSLog(@"RETINA");
    } else {
        // non-Retina display
        NSLog(@"NON-RETINA");
    }

Now, since I'll be using this code a lot, is there a way to avoid repeating it? Maybe creating a protocol, or even subclassing UIImage? I'm not sure how to deal with this, I thought this was done automatically.


Solution

  • If the simulator is seeing the retina images and your device isn't than there might be a problem with case sensitivity. The simulator doesn't care but the actual devices do. Check the names of your images with and without the @2x for differences in upper and lowercases.

    If this doesn't solve your problem (which would be very strange) you can automate the method by adding this to your .m file:

    -(BOOL)retinaScreenResolution {
    if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] &&
        ([UIScreen mainScreen].scale == 2.0))
        return YES;
    else
        return NO;
    

    and call it anywhere in your code with:

    if ([self retinaScreenResolution]) {
        //YES retina
    }
    else {
        //NO
    }