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:
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.
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.
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]
)
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.
来源:https://stackoverflow.com/questions/36786475/loading-a-uiimage-from-xcassets