Search code examples
iosimageretina-display

iOS: manage an universal app


I created an app for iPad, so I have two different version of image, example:

image.png (for iPad 2 or iPad Mini-not retina)
[email protected] (for iPad >= 3 and iPad mini-retina)

It' very easy.

Now I should to create an universal version of my app, so I created my new launch image, icon and some background for iphone size without @2x because I don't want consider iphone 3gs, so I don't set image for iphone with @2x. And it's all ok.

But my app is a game, so I have a lot of images that are fine also for my iphone version. The problem is that iphone 4, 4s, 5 and 5s take image with @2x, but for these device, especially for iphone 4 these image are very big, and I have some memory leak with crashes. Now, my question is: is there a way, if device is retina, to take image that don't have the "@2x"??


Solution

  • Sure, just manage opening the images yourself.

    For instance, I wanted to have a method like "imageNamed" but did not want the caching that imageNamed does, so I wrote my own. This isn't the function you want, but it illustrates the concept and you can make up your own rules. It isn't a huge amount of work to do your own implementation. If you wanted the caching of imageNamed, just use imageNamed to open the image that you eventually decide to use.

    // An "uncached" version of -imageNamed.
    // It looks for the scale-and-idiom suffixes @2x, ~ipad, and @2x~ipad
    // and preferentially loads the specific version if it is available.
    + (UIImage *)imageNamedUncached:(NSString *)name {
        NSString *tryName;
        NSString *nonRetinaiPadTryName = @"";
        NSArray *parts = [name componentsSeparatedByString:@"."];
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            if ([[UIScreen mainScreen] scale] == 1.0) {
                // non-retina
                tryName = [parts[0] stringByAppendingString:@"~ipad"];
                tryName = [tryName stringByAppendingString:@"."];
                tryName = [tryName stringByAppendingString:parts[1]];
            } else {
                // retina
                tryName = [parts[0] stringByAppendingString:@"@2x~ipad"];
                tryName = [tryName stringByAppendingString:@"."];
                tryName = [tryName stringByAppendingString:parts[1]];
                nonRetinaiPadTryName = [parts[0] stringByAppendingString:@"~ipad"];
                nonRetinaiPadTryName = [nonRetinaiPadTryName stringByAppendingString:@"."];
                nonRetinaiPadTryName = [nonRetinaiPadTryName stringByAppendingString:parts[1]];
            }
        } else {
            // iPhone
            if ([[UIScreen mainScreen] scale] == 1.0) {
                // non-retina
                tryName = name;
            } else {
                // retina
                tryName = [parts[0] stringByAppendingString:@"@2x"];
                tryName = [tryName stringByAppendingString:@"."];
                tryName = [tryName stringByAppendingString:parts[1]];        }
        }
        NSString *path = [[NSBundle mainBundle] pathForResource:tryName ofType:nil];
        //NSLog(@"For tryName %@ I get path %@",tryName, path);
        if ( ! path) {
            // If this is a retina iPad we made a non-retina try name -- so check that
            if ( ! [nonRetinaiPadTryName isEqualToString: @""]) {
                path = [[NSBundle mainBundle] pathForResource:nonRetinaiPadTryName ofType:nil];
                //NSLog(@"For non-retina ipad try name %@ I get path %@",nonRetinaiPadTryName, path);
            }
            if ( ! path) {
            // Try the unmodified name
            path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
                //NSLog(@"For base name %@ I get path %@",name, path);
            }
        }
        UIImage *pathImage = [UIImage imageWithContentsOfFile:path];
        return pathImage;
    }