5

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?

Community
  • 1
  • 1
Nerrolken
  • 1,975
  • 3
  • 24
  • 53
  • Assets that are `@3x` won't be found in iOS 7. Are regular assets & `@2x` assets not appearing either? – AdamPro13 May 08 '15 at 17:14
  • 1
    Can you provide more information about the names of the images in question, and the string that you're passing to `-imageNamed:`? – Tim May 08 '15 at 17:14
  • 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 May 08 '15 at 17:23
  • 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 May 08 '15 at 17:24
  • @AdamPro13 The whole thing isn't working. "imageNamed:" returns `nil` for a single file that has 1x, 2x, and 3x versions. – Nerrolken May 08 '15 at 17:26
  • @matt I breakpointed the UIImage object when it is set using "imageNamed:". On iOS 8, by the next line the object has been set, while on iOS 7, that after that line the object is nil. And no, no size classes. – Nerrolken May 08 '15 at 17:27
  • @LyricalPanda matt made two comments. I addressed the second, while I whip up his request in the first. – Nerrolken May 08 '15 at 17:29
  • Well, you didn't say anything about size classes. It is possible to configure an asset catalog so that it uses different images for different size classes, but of course that configuration is not compatible with iOS 7 because it has no size classes and doesn't understand that configuration. – matt May 08 '15 at 17:31
  • @matt Good thought, but I'm not using size classes. (I edited my comment a bit ago to address that part.) – Nerrolken May 08 '15 at 17:33
  • @Nerrolken your problem is solved or not. – Ashok Londhe May 11 '15 at 07:11
  • @AshokLondhe It has been solved. I added an answer, but SO doesn't let you accept your own answer for two days. – Nerrolken May 11 '15 at 16:06
  • @Nerrolken i have not posted any answer or question. so how can i accept. – Ashok Londhe May 12 '15 at 03:34
  • @AshokLondhe Obviously, you CAN'T accept. I can. You asked whether I was still having the problem, and I said no, it was fixed, but I couldn't accept the answer yet because the two days weren't up. – Nerrolken May 12 '15 at 06:37
  • @Nerrolken Yes but you have accepted your own answer. – Ashok Londhe May 12 '15 at 06:39
  • @AshokLondhe You posted that comment 14 hours ago! At the time you posted the comment, I couldn't accept my own answer. Since then, the two days have passed, and I have accepted it. The question is answered, the problem is solved. – Nerrolken May 12 '15 at 06:40
  • @Nerrolken it's good. – Ashok Londhe May 12 '15 at 06:47

2 Answers2

2

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.

Nerrolken
  • 1,975
  • 3
  • 24
  • 53
0

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 :)

Nguyen Tran
  • 158
  • 8