UIImage "imageNamed:" works on iOS 8 but not 7?

There are several answers to SO already about "imageNamed:" not working, but no one turned on my wrinkle:

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

Obviously, everything is connected correctly because it works on iOS 8, but it constantly returns nil for a wide range (maybe 2 dozen) of images on iOS 7. Notably, I moved the assets in AssetCatalog around the start time. Is there any unusual asset catalog behavior on iOS 7 that needs to be considered?

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

  • This problem occurs in real assemblies that are archived and exported to users, as well as in the simulator. Both versions show a difference in the behavior of iOS 7/8.
  • Images are PNGs, and the Asset Catalog contains 1x, 2x, and 3x versions of each.
  • I confirmed the coincidence of spelling and capitalization (later confirmed this while working on iOS 8).

Today, most answers to SO are bugs that would prevent it from working at all. Can someone help with accessing images that only works on iOS 8?

+5
source share
2 answers

Yes. Apparently, the solution was that if your set of images in the Asset Catalog had a file suffix in its name, iOS 7 would work. We have several versions of each file in our asset directory, the same image, but for different purposes, so my colleague called them the old file name as follows:

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

Obviously, iOS 8 is smart enough to recognize that "RedBall.png - Small" is the name, while iOS 7 does not know what to do when ".png" is right in the middle.

Renaming files to ...

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

... seems to be doing the trick.

+2
source

You can follow these steps:

  • Clean the project (Cmd + Shift + K) and run it again.

  • Add another image (not @ 2x, @ 3x) and check if it works or not.

  • Create a new project and verify that UIImage imagedName can work with iOS7 / 8.

With some problems, we don’t need to try to find the root cause, just skip it and go :)

0
source

All Articles