5
votes

There are several answers on SO already about "imageNamed:" not working, but none have included my particular wrinkle:

I'm getting nil from "imageNamed:" on iOS 7, but the same build works on iOS 8.

Obviously, everything is hooked up properly because it does work on iOS 8, but it consistently returns nil for a wide range (maybe 2 dozen) of images on iOS 7. Notably, I moved the assets to an AssetCatalog around the time this started. Is there some unusual behavior of asset catalogs on iOS 7 that has to be accounted for?

Here are some details, based on the answers to similar questions on this site:

  • This problem appears in actual builds that are archived and exported for users, as well as in the simulator. Both versions show the iOS 7/8 split in behavior.
  • Images are PNGs, and the Asset Catalog contains 1x, 2x, and 3x versions of each.
  • I've confirmed the spelling and capitalization matches (further confirmed by it working on iOS 8).

By far the majority of the answers on SO are for bugs that would prevent it from working at all. Can anyone help with an image call that only works on iOS 8?

2
Assets that are @3x won't be found in iOS 7. Are regular assets & @2x assets not appearing either?AdamPro13
Can you provide more information about the names of the images in question, and the string that you're passing to -imageNamed:?Tim
The problem is that you are not giving enough info to reproduce the problem. When I use imageNamed: in iOS 7/8 for an image in asset catalog, it works. Try to make a tiny little project that shows the problem, and post it where we can look at it.matt
Also: how do you know that it is nil? Oh, and are you by any chance doing something with size classes in iOS 8?matt
@AdamPro13 The whole thing isn't working. "imageNamed:" returns nil for a single file that has 1x, 2x, and 3x versions.Nerrolken

2 Answers

2
votes

Huh. So apparently the solution was that if your imageset in the Asset Catalog has a file suffix within its name, iOS 7 gets tripped up. We've got several versions of each file in our Asset Catalog, the same image but for different uses, so my coworker had named them with the old filename like so:

"RedBall.png - Small" (includes 25px, 50px, 75px)
"RedBall.png - Large" (includes 150px, 300px, 450px)
"BlueBall.png - Small"
"BlueBall.png - Large"

Apparently iOS 8 is smart enough to recognize that "RedBall.png - Small" is the name, while iOS 7 doesn't know what to do when ".png" is right there in the middle.

Renaming the files to...

"RedBall - Small"
"RedBall - Large"
"BlueBall - Small"
"BlueBall - Large"

...seems to do the trick.

0
votes

You can try below steps:

  1. Clean your project (Cmd+Shift+K) and run again.

  2. Add an other image (not @2x, @3x) and check it work or not.

  3. Create a new project and check UIImage imagedName can work with iOS7/8.

With some problems, we don't need try finding a root cause, just skip it and go :)