1

I'm attempting to load images from my LaunchImage image set in a .xcassets file, but I don't want do use imageNamed:, as it wastes memory and the image only needs to be displayed for a couple of seconds during initial launch.

I have tried multiple approaches to this, but so far I have only been able to load them using imageNamed:.

This works:

[UIImage imageNamed:@"LaunchImage-700-568h.png"]

This doesn't work (returns null):

[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"LaunchImage-700-568h" ofType:@"png"]]

Is there any way to do this without adding the resources explicitly to the target (aside from the assets file)? Thanks!

rebello95
  • 8,486
  • 5
  • 44
  • 65

1 Answers1

2

Looks like the answer to your question is no.

According to XCAssets documentation.

Each set in an asset catalog has a name. You can use that name to programmatically load any individual image contained in the set. To load an image, call the platform specific class method, passing in the name of the set that contains the image. The OS will load the image from the set that is most appropriate for the current scale factor. The platform method for iOS is imageNamed:. For OS X the platform method is imageNamed:

So we must use [UIImage imageNamed:] method to load images from XCAssets catalog on iOS.

See similar question and answers here.

Community
  • 1
  • 1
salabaha
  • 2,468
  • 1
  • 17
  • 18
  • Thanks for the response. Is there any way to do this without having to add the file as a resource outside of the Xcode assets file? I'd like to keep the app's file size down if possible, and that adds up with different launch files for different screens. – rebello95 Mar 09 '15 at 21:26
  • Too bad that they don't have a better way to do this... Thanks for the answer. – rebello95 Mar 11 '15 at 04:00