1

In previous iOS SDK, [UIImage imageNamed:] can determine the image to load automatically without giving the file extension, for example:

[ivTest setImage:[UIImage imageNamed:@"testImage"]];

It will search for appropriate image to load (i.e. testImage.jpg & testImage@2x.jpg). But for iOS 8, it sometimes cannot identify the correct image to load, and just returns null.

I have to specify the extension:

[ivTest setImage:[UIImage imageNamed:@"testImage.jpg"]];

But this happens only in same rare cases.

Given that I have only the following images in the project:

  • testImage.jpg
  • testImage@2x.jpg

No other images with similar names. How can I prevent this? Should I specify extension for all my imageNamed: ?

Raptor
  • 53,206
  • 45
  • 230
  • 366
  • 1
    Are the images in a .xcassets? – dasdom Sep 17 '14 at 07:56
  • 1
    Place the images inside images.xcassets. – Sandeep Sep 17 '14 at 07:57
  • No. The images are not in `.xcassets`. How does this relate? – Raptor Sep 17 '14 at 08:00
  • "On iOS 4 and later, if the file is in PNG format, it is not necessary to specify the .PNG filename extension. Prior to iOS 4, you must specify the filename extension." – KudoCC Sep 17 '14 at 08:07
  • @KudoCC I'm working on iOS 8. That means for all JPEG I have to specify the filename extension? – Raptor Sep 17 '14 at 08:09
  • 1
    Yes, I think you have to specify the filename extension for all JPEG. That paragraph above is reference from apple document. – KudoCC Sep 17 '14 at 08:13
  • @KudoCC Please put this as answer. By the way, I still can't figure out how `.xcassets` relates to this case (as they have 2 upvotes in total) – Raptor Sep 17 '14 at 08:15
  • @Raptor using an assets catalog is much easier to manage. You no longer have to worry about calling them 2x and so on. You just drop the correctly sized image into the correct slot and give the group of images a name. Also all the images are then in one place managed by the project. To start using it just go to the build page and click "Use Asset Catalog". It even imports all your existing images. – Fogmeister Sep 17 '14 at 08:15
  • with `imageNamed` I don't have to worry about `@2x` too. – Raptor Sep 17 '14 at 08:16
  • 1
    @Raptor yes, you do. You have to have the files named "blah" and "blah@2x". It's up to you though. It's easy to switch to. It's been around for two years. It makes managing images easier. It doesn't require any code changes. It will help with iOS8 3x images. But I can understand if you don't want to use it... no wait... lol – Fogmeister Sep 17 '14 at 08:17
  • 1
    @Raptor it also helps to validate your icon images and launch images. – Fogmeister Sep 17 '14 at 08:19
  • Clarification needed: while `imageNamed` can auto select `blah`, `blah@2x` or `blah@3x`, advantages about Asset Catalog is to make files easier to manage (provides validation on icons & launch images as well), and codes need not to be changed (also no need to specify the file extension in code). So what are the disadvantages? – Raptor Sep 17 '14 at 08:24
  • I'm still using old tricks in iOS4. Modern Xcode 6 is too new for me LOL – Raptor Sep 17 '14 at 08:25
  • 1
    @Raptor I'd suggest opening a new project and taking a look. You'll get a lot more from it than from a comment on Stack Overflow. It uses the same code but instead of looking for "blah", "blah@2x" and "blah@3x" it looks for the image group in the assets file called "blah". In the group you can do all sorts. Specifying resizable images. Device specific (not just resolution specific) images. Specifying template rendering. Etc... There really isn't a reason to not use it. It would be like not using Interface Builder... but I guess you don't use that either. – Fogmeister Sep 17 '14 at 08:27
  • I use Asset Catalog for Icons & Launch Images, and use Storyboard for building UI; will try Asset Catalog for layout images. – Raptor Sep 17 '14 at 08:29
  • 1
    @Raptor In that case, in the asset catalog. Right click the navigator pane (left hand side) and click "Import From Project". This will find your project images and import them for you. – Fogmeister Sep 17 '14 at 08:30

1 Answers1

3

Reference from here

On iOS 4 and later, if the file is in PNG format, it is not necessary to specify the .PNG filename extension. Prior to iOS 4, you must specify the filename extension.

So the answer is you have to specify the filename extension for all JPEG.

About Asset Catalog, well, to tell the truth I don't have any ideas about that, let's study it later.

KudoCC
  • 6,912
  • 1
  • 24
  • 53