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

StackOverflow https://stackoverflow.com/questions/13425834

Вопрос

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

image@2x.png

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 image@2x.png 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!

Это было полезно?

Решение

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 test@2x.png). 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"];

Другие советы

I think that the best you could do is to load different images on the iPod so you can guarantee that there does not exist the @2x version. This way you could save RAM but actually the application would be heavier because of the image repetition...

Hope it helps! See you!

I would write a category on UIImage with a function named something like

myImageNamed:(NSString*)name

then use UIDevice to determine the current device and search your bundle for the image you'd like to use.

It would look something like this:

- (UIImage*)myImageNamed:(NSString*)name
{
    if (!notIpodTouch) {
       return [self imageNamed:name];
    }

    //search through mainBundle for file with passed in name
    //return that

}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top