iosobjective-cxcodeuiimageimagenamed

Getting launch image from XCAssets using imageWithContentsOfFile:


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!


Solution

  • 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.