3

I have a universal app with different default images:

Default.png

Default@2x.png

Default-Portrait@2x~ipad.png

and so on. How can I determine programmatically at runtime which file was used? I tried this:

    UIImage *imgUsed = [UIImage imageNamed:@"Default"];

but this always returns Default.png regardless of which simulator I am using (the correct image is displayed on startup in the simulator based on which screen size is being used).

Thanks.

Community
  • 1
  • 1
waltcrit
  • 53
  • 1
  • 5
  • I have to ask, _why_ do you need to know this? Just because there may be a better way of achieving what it is you want. – jrturton Nov 17 '12 at 17:40
  • I want to extend the default screen on startup to make a "Loading..." screen with the default image as the background. It would only happen at the first run (or when installing a new version), so not frequently. – waltcrit Nov 17 '12 at 17:46
  • I would comment that `imageNamed:` will cause the image to be cached for the life of your application (undesirable for the splash screen) - use `imageWithContentsOfFile:` - (get the path with `[[NSBundle mainBundle] pathForResource:name ofType:@"png"]`) – bshirley Dec 18 '12 at 16:00

2 Answers2

1

Create a "loading" view controller with an image view - one iPhone and one iPad version. In the each one, set the image as Default or default-iPad - the retina / non-retina version will automatically be chosen for you.

Add the loading label or activity indicator to the view controller as well.

When you need to, present this view controller - you can tell if you are running on iPhone or iPad using the UI_USER_INTERFACE_IDIOM() macro (see here) and load the appropriate one.

Community
  • 1
  • 1
jrturton
  • 118,105
  • 32
  • 252
  • 268
  • This works for `Default.png` and `Default@2x.png` but not for longer filenames (e.g. `Default-Portrait@2x~ipad.png`). But this is very close...! – waltcrit Nov 17 '12 at 23:52
0

The way you do this is like reverse engineering. You find out if the screen scale is 1 or 2 (retina or not) and what the dimension of the device is (iPad, iphone3.5 screen, iphone4.0 screen), then you recreate the base image name using this information.

There is no api whereby the system tells you this information.

EDIT: right, you can use the UI_USER_INTERFACE_IDIOM macro per @jrturton suggests to determine iPad/iPhone, but you still need to look for the phone size to know if the 3.5 or 4 inch high image was used.

David H
  • 40,852
  • 12
  • 92
  • 138