从xcassets加载UIImage [英] Loading a UIImage from xcassets

查看:102
本文介绍了从xcassets加载UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Image.xcassets文件夹加载启动图像,但无济于事。还有其他



编辑2:我的最终代码:

   - (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];
}
}

}



请注意它仅适用于Portrait。

解决方案

您无需指定图像的大小名称。它将自动加载最适合运行应用程序的设备的大小。所以你的代码应该是。

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

其中default是来自 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:

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.

这篇关于从xcassets加载UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆