【问题标题】:iOS universal app which launch image启动图像的 iOS 通用应用程序
【发布时间】:2012-12-27 13:01:18
【问题描述】:

我正在编写一个支持旋转的通用应用程序。当应用启动时,它需要从互联网下载一些数据,所以我推送了一个带有活动指示器的 UIViewController,因为它不可能有动画启动图像或添加标签或对象。

我希望“正在加载”的 VC 具有与启动相同的 bg 图像,但是,因为它是一个通用应用程序,我不能设置一个简单的 [UIImage imageNamed:@"Default.png"] 因为它可以运行在 iPhone 或 iPad 上以及 iPad 是否可以纵向或横向启动(iPhone 应用程序始终以纵向启动)。

问题是:有办法知道哪个 Default.png 已用作启动图像吗?可以是

  • Default.png(@2x 如果视网膜)
  • Default-Portrait.png(@2x 如果视网膜)
  • Default-Landscape.png(@2x 如果视网膜)
  • 默认-568h@2x.png

如果现在有办法,我会检查 currentDevice 和方向并手动设置 imageNamed。

谢谢, 最大

【问题讨论】:

    标签: ios uiviewcontroller


    【解决方案1】:

    纵向和横向没有后缀。您必须使用 [[UIDevice currentDevice] orientation] 手动检查方向。

    为了显示 iPad 和 iPhone/iPod Touch 的不同图像,您可以在 iPad 图像的末尾添加~ipad,在 iPhone/iPod Touch 图像的末尾添加~iphone。示例:

    Default~iphone.png 将在 iPhone/iPod Touch 上加载,而 Default~ipad.png 将在 iPad 上加载:

    [UIImage imageNamed:@"Default.png"];
    

    尽管如此,iPhone 5 也没有说明符。因此,您必须检查[[UIScreen mainScreen] bounds].size.height 并再次手动加载UIImage


    完整(未经测试)示例:

    UIImage *image;
    
    if ([UIScreen mainScreen].bounds.size.height == 568.0)
    {
        if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
        {
            image = [UIImage imageNamed:@"Default-568h-Landscape"];
        }
        else
        {
            image = [UIImage imageNamed:@"Default-568h-Portrait"];
        }
    }
    else
    {
        if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
        {
            image = [UIImage imageNamed:@"Default-Landscape"];
        }
        else
        {
            image = [UIImage imageNamed:@"Default-Portrait"];
        }
    }
    // check suggested by Guy Kogus
    if (image == nil) image = [UIImage imageNamed:@"Default"];
    

    在 cmets 中回答您的问题:

    不,您无法查询运行时使用的启动图像。

    【讨论】:

    • 纵向或横向有后缀。如果你想看清楚,创建一个虚拟项目并有 2 个图像,一个 1024x768 和另一个 768x1024。转到项目设置并拖动 Launch Images 部分中的图像,您会看到 Xcode 自动使用所需的 -Landscape 或 -Portrait 后缀命名它们(或者就足够了?)。
    • 是的,但我认为在使用-[UIImage imageNamed:]时不起作用
    • 不,您必须手动添加这些后缀。但这相当简单,您只需要使用状态栏的方向即可。
    • 在你的代码末尾,我建议添加一个if 语句来检查是否仍然没有图像,在这种情况下只需使用image = [UIImage imageNamed:@"Default.png"];
    • 另外,图像名称中不需要 .png。 UIImage 会自动处理。
    【解决方案2】:

    @2x 后缀是自动选择的,因此您无需担心。您需要执行一些检查:

    - (NSString *)defaultImage
    {
        NSString *imageName = nil;
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
        {
            if ([UIScreen mainScreen].bounds.size.height == 568.0)
            {
                imageName = @"Default-568h";
            }
            else
            {
                imageName = @"Default";
            }
        }
        else // ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
        {
            if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation))
            {
                imageName = @"Default-Landscape";
            }
            else // if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation))
            {
                imageName = @"Default-Portrait";
            }
        }
        return [UIImage imageNamed:imageName];
    }
    

    【讨论】:

    • 谢谢,看我对博A回答的评论。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 2015-03-26
    相关资源
    最近更新 更多