Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading a UIImage from xcassets

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.

like image 305
Guy Lowe Avatar asked Feb 07 '23 05:02

Guy Lowe


1 Answers

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.

like image 86
Jelly Avatar answered Feb 13 '23 06:02

Jelly