4

I'm trying to load a launch image from the Image.xcassets folder but to no avail. There are other answers (and this one) on SO that purport to answer it but their main solution, to simply load an image like so

UIImage *image =  [UIImage imageNamed:@"Default@2x"];

returns nil for me.

The filename is named correctly and the project is setup to use the assets.

Does anyone have any idea how I do this or what I could be doing wrong?

Thanks in advance.

EDIT:

screenshot of my assets folder

EDIT 2: my final code:

-(void) loadSplashImage{
if ([self isiPad]){
    self.imageViewSplash.image =  [UIImage imageNamed:@"Default-Portrait"];
}
else{
    if (self.view.frame.size.height == 480){
        self.imageViewSplash.image =  [UIImage imageNamed:@"Default"];
    }
    else if (self.view.frame.size.height == 568){
        self.imageViewSplash.image =  [UIImage imageNamed:@"Default-568h"];
    }
    else if (self.view.frame.size.height == 667){
        self.imageViewSplash.image =  [UIImage imageNamed:@"Default-667h"];
    }
}

}

Please note it works for Portrait only.

Community
  • 1
  • 1
Guy Lowe
  • 2,115
  • 1
  • 27
  • 37

3 Answers3

7

You dont have to specify the size of the image in your name. It will automatically load the size that best fits for the device that runs the app. So your code should be.

UIImage *image =  [UIImage imageNamed:@"Default"];

where default is the name of the resource from xcassets, the one you see on the leftside list.

Jelly
  • 4,522
  • 6
  • 26
  • 42
  • Thanks Jelly but I'm still getting nil. – Guy Lowe Apr 22 '16 at 06:42
  • Well post a screenshot of your `xcassets ` so we can see how it looks like – Jelly Apr 22 '16 at 06:43
  • 1
    You have to put them in xcode, because they won't be picked up if you just put them in a folder. – Jelly Apr 22 '16 at 06:56
  • You should have a `Assets.xcassets` file in your xcode project. Open that and there should your resources go in. Check more about assets catalogs here https://developer.apple.com/library/ios/recipes/xcode_help-image_catalog-1.0/_index.html . – Jelly Apr 22 '16 at 06:59
  • Adding a reference to them in XCode did the trick. Thanks a lot. – Guy Lowe Apr 22 '16 at 07:00
1

Here's the way to get LaunchImage name.

Xcode 5 & Asset Catalog: How to reference the LaunchImage?

Getting image name from info.plist ([[NSBundle mainBundle] infoDictionary])

Community
  • 1
  • 1
hstdt
  • 5,652
  • 2
  • 34
  • 34
0

You should have 2 files:

(1) Default.png (or any other image format) — Non retina

(2) Default@2x.png — Retina

Now, to get this image, you will not have to use @2x at the end of the file name. Use just name of that image asset.

NSPratik
  • 4,714
  • 7
  • 51
  • 81