Loading a UIImage from xcassets

蓝咒 提交于 2019-12-07 07:16:29

问题


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.


回答1:


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.




回答2:


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])




回答3:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!