2

I am attempting to load an image (.png) into the backgroundView of a UITableViewCell. However, the image is not displaying. Based on log outputs of the image returning NULL, it seems to be an issue of initializing the image.

Things I have tried/checked:

  1. The filename is spelled and capitalized correctly.

  2. The file was appropriately added to Xcode and is visible in Copy Bundle Resources

  3. The file's Target Membership box is checked in the File Inspector

  4. I updated the file, so to be sure, I deleted app from the simulator as well as the contents of the DerivedData folder.

  5. I restarted XCode.

  6. I tried loading the image via:

    [UIImage imageNamed:@"cell-bg.png"] 
    

    as well as

    [UIImage imageNamed:@"cell-bg"]
    
  7. I also tried loading from the bundle:

    NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *imagePath = [mainBundle pathForResource:@"cell-bg" ofType:@"png"];
    NSLog(@"path: %@", imagePath);
    UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
    

For this last one, the NSLog produces a full path in the output, but when I perform another NSLog for image it returns NULL.

Apple's UIImage Class Reference states under the -imageWithContentsOfFile: method:

Return Value A new image object for the specified file, or nil if the method could not initialize the image from the specified file.

So, could there be something wrong with the file? How could I diagnose and correct the issue?

kiks
  • 105
  • 1
  • 7
  • Is the image correctly viewable in other apps, e.g. Preview? If you have a non-nil `imagePath` then the file is in the bundle. If you substitute a different arbitrary image, do you have the same result? – FluffulousChimp Sep 18 '12 at 10:48
  • Good suggestion. The image displays both in Preview and within Xcode when I select the file from the hierarchy. Strangely enough, as per your suggestion, I substituted in a different file and voila. I will remake the file I need and try it all over again. To be continued.. (Still don't know exactly why the file is having problems) – kiks Sep 18 '12 at 11:01
  • UPDATE: A new file did the trick. Re-creating the .png file I needed seems to be my solution here. If anyone has any suggestions as to how to further diagnose image file issues (b/c not every file is so easily reproducible), please feel free to share! THANKS, ALAN! – kiks Sep 18 '12 at 11:20
  • No problem. see [this](http://stackoverflow.com/questions/2408027/png-file-validation) for png validation – FluffulousChimp Sep 18 '12 at 11:23

0 Answers0