Search code examples
iphoneiosuikitretina-displayipod-touch

Any way to force UIKit to use standard (not @2x) images on certain devices, even when @2x images are available?


I'm working on a project that uses a lot of detailed graphics. Because of the amount of sprites, backgrounds, etc. displayed at all times, it has a large memory footprint. We use cocos2D for the animations and sprites and UIKit for the UI, menus, etc.

I would like to be able to reduce memory usage on iPod Touches (which have less memory than the equivalent iPhones) by forcing UIKit to use standard resolution images if the device is an iPod Touch with retina display. I'm able to set a flag in cocos2d the forces loading of SD images, but I haven't found a similar option or workaround for UIKit.

So to summarize, the project is a universal binary that has all images in both SD and retina flavors, like this:

image.png

[email protected]

On iPhone 4, 4S, and 5 (all with retina displays) the default UIKit behavior of loading the @2x version of the images is desired.

On iPod Touch 4th gen (and possibly 5th gen) I would like to force UIKit to load the SD image.png files, even though there are [email protected] files available as well.

This is to reduce memory footprint on these devices that have retina displays, but half the RAM as the phones.

If anyone has any ideas or workarounds to enable this, I'd really appreciate it!


Solution

  • Adding my comment as an answer,

    You might have to create your own methods to fetch images based on device. You shouldn't be naming images with @2x in that case. Your method should fetch image based on device and retina display and give back the name of the image which can be used to create UIImage objects. This method can be a category added on UIImage.

    For eg:-

    [UIImage testImageNamed:@"test.png"];
    

    and implement testImageNamed method as a category in UIImage which returns testRetina.png or test.png based on device and retina/non-retina resolution.

    Update: Alternatively, you can also try with the following for iPod touch device in this category so that you can keep images with @2x itself(test.png and [email protected]). I am not sure if this will work or not. I am assuming that in case fileName has only "test", this might fetch test.png itself but I haven't tried this. This is definitely worth a try.

    NSString *fileLocation = [[NSBundle mainBundle] pathForResource:fileName ofType:extension];
    NSData *imageData = [NSData dataWithContentsOfFile:fileLocation];
    [UIImage imageWithData:imageData];
    

    Add the above only for iPod touch and for other devices it can return normal [self imageNamed:@"test.png"];