Gotcha: Case Sensitive PNG Filename

Here’s one that will trip you up if you aren’t careful…look at this code:

timerButton = [[UIButton alloc] initWithFrame:CGRectMake(104, 410, 50, 50)];    
 
// Notice the filename "timer.png"
[timerButton setBackgroundImage:[UIImage imageNamed:@"timer.png"] 
    forState:UIControlStateNormal];
 
[timerButton addTarget:self action:@selector(buttonPressed:) 
   forControlEvents: UIControlEventTouchUpInside];
 
[self.view addSubview:timerButton];

Simple enough, create a button, use the image timer.png for the normal state, and all is well…if you run this code in the simulator. Problem is, if the actual filename of the image is Timer.png the png file will not appear when running this code on device.

If you haven’t run into this while developing an iPhone app, you can’t fully appreciate how time consuming it can be to track this down.

Note: I ran into this while working with Xcode 3.2.1 and building for iPhone OS 3.1.2

  1. The code works in the simulator because your Mac hard drive was formatted with case insensitive filesystem.

  2. As the other comments said, the Mac file system is not case sensitive but the iPhone is. So it is important to use matching capitalization when accessing any resource file.

  3. For me I gave the same file name of the Image as case sensitive…….but I dont know why Image is not displaying in the device.
    But its working fine in Simulator.

    Any one’s help is appreciated.

    • Monish, here are a few things to try: do a “clean” of all the project files and rebuild; remove the application from the device before re-installing through Xcode; remove the file and add back to the project; rename the file to something numeric 123.png to see what happens.

  4. hey john I tried the things that u suggested before.Even though the inages are not displayed in the device.Would please suggest any other solution for this.I am wasting lots of time on this.

    Anyone’s help will be appreciated.

    Thank you,
    Monish Kumar.

  5. I had a similar thing just today. I will usually change the extension of an image when i download it if i want to just because it makes no difference, the computer will still display it fine.
    But when i used an image i had done that too (i forgot that though) my app worked in the simulator but just crashed instantly on star on my device and i spent ages pulling my code apart to find the memory leak or bug, until i had worked out the problem was with that image.

Comments are closed.