如何从Images.xcassets中获取LaunchImage的图片
前言:
最新在写一个APP启动页广告,我的思路是在启动页显示完毕之后马上以同样的图片盖上去,同时请求服务器上的广告图片,当图片缓存下来之后就替换掉图片。据我分析网易新闻等APP就是这样实现的吧!
Images.xcassets是Xcode5之后才开始出现的,当我想从Images.xcassets获取启动图片的时候,图片没有获取到。我在写demo的时候从以前工程中拖动了两个启动页测试,一切正常。当我放在正式工程中去的时候发现图片获取不到了。最后我发现原来是图片命名的问题,我以前老工程图片是LaunchImage-700-568h形式的命名,直接就能取到图片,但是新工程不是这种命名方式了。因此我们可以通过使用以下的名称获取到对应图片。程序的appicon直接获取也是获取不到的。
说明:
LaunchImage的名字组成形式是这样的:
[LaunchImage在Assets Catalog中的注册名字]-[iOS版本]-[屏幕方向]-[屏幕高度][比例].png
如下:
iOS | Display | Image Name |
---|---|---|
iOS5,6 | 3.5inch | LaunchImage |
iOS5,6 | 3.5inch Retina | LaunchImage@2x |
iOS5,6 | 4.0inch Retina | LaunchImage-568h@2x |
iOS7,8 | 3.5inch Retina | LaunchImage-700@2x |
iOS7,8 | 4.0inch Retina | LaunchImage-700-568h@2x |
iOS8 | 4.7inch Retina | LaunchImage-800-667h@2x |
iOS8 | 5.5inch Retina Portrait | LaunchImage-800-Portrait-736h@3x |
iOS8 | 5.5inch Retina Landscape | LaunchImage-800-Landscape-736h@3x |
代码示例:
+ (UIImage *)getTheLaunchImage
{
NSString *defaultImageName = @"LaunchImage";
NSInteger osVersion = floor([[[UIDevice currentDevice] systemVersion] floatValue])*100;
NSInteger screenHeight = CGRectGetHeight([UIScreen mainScreen].bounds);
// 3.5inch
if (screenHeight < 568) {
if (osVersion >= 700) {
defaultImageName = [NSString stringWithFormat:@"%@-700",defaultImageName];
} else {
defaultImageName = [NSString stringWithFormat:@"%@",defaultImageName];
}
}
// 4.0inch
else if(screenHeight < 667){
if (osVersion >= 700) {
defaultImageName = [NSString stringWithFormat:@"%@-700-568h",defaultImageName];
} else {
defaultImageName = [NSString stringWithFormat:@"%@-568h",defaultImageName];
}
}
// 4,7inch
else if (screenHeight < 736) {
defaultImageName = [NSString stringWithFormat:@"%@-800-667h",defaultImageName];
}
// 5.5inch
else{
NSString *orientation = @"";
switch ([[UIApplication sharedApplication] statusBarOrientation]) {
case UIInterfaceOrientationUnknown:
case UIInterfaceOrientationPortrait:
case UIInterfaceOrientationPortraitUpsideDown:
orientation = @"Portrait";
break;
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:
orientation = @"Landscape";
break;
default:
break;
}
defaultImageName = [NSString stringWithFormat:@"%@-800-%@-736h",defaultImageName,orientation];
}
return [UIImage imageNamed:defaultImageName];
}
—————-华丽的分割线 2015年12月2日更新—————-
在iOS9之后,在iOS9中的图片依然是以800为标准的,因此以上代码已经更新。
另外注意,如果项目没有开启高分模式,那么图片只能以最大以700为标准(因为现在很少有不使用高分模式的APP,因此代码中我没有进行编写),也就是如下:
{
UILaunchImageMinimumOSVersion = "7.0";
UILaunchImageName = "LaunchImage-700";
UILaunchImageOrientation = Portrait;
UILaunchImageSize = "{320, 480}";
},
{
UILaunchImageMinimumOSVersion = "7.0";
UILaunchImageName = "LaunchImage-700-568h";
UILaunchImageOrientation = Portrait;
UILaunchImageSize = "{320, 568}";
}
如果大家想通过代码查看图片标准可以使用以下代码:
NSInteger osVersion = floor([[[UIDevice currentDevice] systemVersion] floatValue])*100;
NSString *key;
if (osVersion >= 700) {
key = @"UILaunchImages";
} else {
key = @"UILaunchImageFile";
}
NSArray *array = [[[NSBundle mainBundle] infoDictionary] valueForKey:key];
NSLog(@"%@",array);
—————-华丽的分割线 2016年8月19日更新—————-
看了“神话先生”的的评论,感觉到自己写的这个太low了。推荐使用神话先生的做法,这样就去除了版本号的判断,更加方便。
+ (UIImage *)getTheLaunchImage
{
CGSize viewSize = [UIScreen mainScreen].bounds.size;
NSString *viewOrientation = nil;
if (([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown) || ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait)) {
viewOrientation = @"Portrait";
} else {
viewOrientation = @"Landscape";
}
NSString *launchImage = nil;
NSArray* imagesDict = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
for (NSDictionary* dict in imagesDict)
{
CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]);
if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]])
{
launchImage = dict[@"UILaunchImageName"];
}
}
return [UIImage imageNamed:launchImage];
}
Demo传送门->https://github.com/ianisme/GetLaunchImage
25 评论
这个还有问题。新出来的设备 尺寸是一样的。xsmax 和xr 。这样for 循环里面 永远取出是一张图片。
@匿名 解决了么, 也发现这个问题了
这个给力
“`
CGSize viewSize = self.view.frame.size;
NSString *viewOrientation = @”Portrait”; //横屏请设置成 @”Landscape”
NSString *launchImage = nil;
NSArray* imagesDict = [[[NSBundle mainBundle] infoDictionary] valueForKey:@”UILaunchImages”];
for (NSDictionary* dict in imagesDict)
{
CGSize imageSize = CGSizeFromString(dict[@”UILaunchImageSize”]);
if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrientation isEqualToString:dict[@”UILaunchImageOrientation”]])
{
launchImage = dict[@”UILaunchImageName”];
}
}
UIImageView *launchView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:launchImage]];
launchView.frame = self.view.bounds;
launchView.contentMode = UIViewContentModeScaleAspectFill;
“`
我是这样子取出来的。
@神话先生 你好,我想问下这个key:UILaunchImages是怎么知道的?
该方法在模拟器上不能获取到LaunchImage,我用的是Xcode Version 7.3 (7D175)
@qingsong 能否发个Demo,我看下原因
@ian 你的Blog下面的demo就不行的。
@ian 你的Blog下面的demo就不行的。
@qingsong 问题已经解决,再试试
你这种方式我觉得不是很方便,我通过摸索发现,build后app包里面有一个info.plist,其中有个UIlaunchImages的array,可以通过遍历匹配屏幕尺寸获得图片名称,然后直接imageNamed:方法取得图片。
@woshiqyb 你的这个方法很棒!
Thanks for ones marvelous posting! I truly enjoyed reading it,
you might be a great author. I will be sure to
bookmark your blog and will come back someday.
I want to encourage continue your great job, have a nice day!
@พันธุ์มันสำปะหลัง @พันธุ์มันสำปะหลัง : Think you.
交个朋友吧,我也是做开发的,不过确实很喜欢你的写作风格
@刘亮 谢谢!当然可以
额 没玩过
你是越捣鼓越高深了!
@小年 还是比较浅
@ian 好吧。。。
好吧!原谅我英语不大懂 点击Notes 看着是去年的 以为没更新呢。。。。
@木木 点击首页 亲
@木木 其他的 About 关于?
Guestbook 留言板?
Links连接
Video视频?
Internet网络?
iOS苹果系统? 你再多写几个我就不知道了
技术文,看不懂。
@an9 额 这是iOS开发
分享您的想法?
撰写评论