【问题标题】:Xcode 5 & Asset Catalog: How to reference the LaunchImage?Xcode 5 & Asset Catalog:如何引用 LaunchImage?
【发布时间】:2013-10-07 02:27:57
【问题描述】:

我正在使用 Xcode 5 的资产目录,我想使用我的 LaunchImage 作为我的主视图的背景图像(一种非常常见的做法,可以使从“加载”到“加载”的过渡看起来平滑)。

我想在资产目录中使用相同的条目来节省空间,而不必在两个不同的图像集中复制图像。

但是,调用:

UIImage *image = [UIImage imageNamed:@"LaunchImage"]; //returns nil

【问题讨论】:

    标签: ios xcode ios7 xcode5 asset-catalog


    【解决方案1】:

    LaunchImages 是特殊的,实际上并不是设备上的资产目录。如果您查看使用 iFunBox/iExplorer/etc(或在模拟器上,或在构建目录中),您可以看到最终名称,然后编写代码来使用它们 - 例如。对于 iOS7-only iPhone-only 项目,这将设置正确的启动图像:

    NSString *launchImage;
    if  ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) &&
         ([UIScreen mainScreen].bounds.size.height > 480.0f)) {
        launchImage = @"LaunchImage-700-568h";
    } else {
        launchImage = @"LaunchImage-700";
    }
    
    [self.launchImageView setImage:[UIImage imageNamed:launchImage]];
    

    我把它放到 viewDidLoad 中。

    这不是很理想,如果 Apple 能给我们一个很好的 API 来做这件事,那就太好了。

    【讨论】:

    • 这对我有用,但我真的希望有一种更简单的方法来引用启动图像。
    • 可能已在 Xcode 5.0.2 中修复 - 见下文,似乎对我来说只需引用“LaunchImage.png”
    • @Adam 如果这是真的,我会很高兴的!我刚刚在 iphone5s/xcode5.0.2/ios7.0.4 上试过,[UIImage imageNamed:@"LaunchImage.png"] 给我 nil。
    • @JosephH 嗯。也许它需要一个新创建的项目?这是在 Xcode 5.0.2 中创建的项目,唯一更改为默认设置是“禁用 ARC”。它工作得很好:)。我会看看能不能找到其他东西,但想不出还有什么我可能改变了
    • 我尝试了类似的代码,但使用了“Default”和“Default-568h”(原始资源文件名)。在查看导出的应用程序包后,我意识到 Xcode 将名称更改为“LaunchImage-700*”。
    【解决方案2】:

    这是 LaunchImage 的(几乎)完整列表(不包括没有状态栏的 iPad 图像):

    • LaunchImage-568h@2x.png
    • LaunchImage-700-568h@2x.png
    • LaunchImage-700-Landscape@2x~ipad.png
    • LaunchImage-700-Landscape~ipad.png
    • LaunchImage-700-Portrait@2x~ipad.png
    • LaunchImage-700-Portrait~ipad.png
    • LaunchImage-700@2x.png
    • LaunchImage-Landscape@2x~ipad.png
    • LaunchImage-Landscape~ipad.png
    • LaunchImage-Portrait@2x~ipad.png
    • LaunchImage-Portrait~ipad.png
    • LaunchImage.png
    • LaunchImage@2x.png
    • LaunchImage-800-667h@2x.png (iPhone 6)
    • LaunchImage-800-Portrait-736h@3x.png(iPhone 6 Plus 纵向)
    • LaunchImage-800-Landscape-736h@3x.png(iPhone 6 Plus 横向)
    • LaunchImage-1100-Portrait-2436h@3x.png(iPhone X 纵向)
    • LaunchImage-1100-Landscape-2436h@3x.png(iPhone X 横向)

    【讨论】:

    • 谁知道没有状态栏的 iPad 图片?
    • @Mohamed Hafez:Pichirich 实际上确实将它们包含在他的列表中。它们是 LaunchImage-Portrait~ipad.png、LaunchImage-Portrait@2x~ipad.png、LaunchImage-Landscape~ipad.png 和 LaunchImage-Landscape@2x~ipad.png。
    • 数字 700 和 800 是什么意思?
    • 我明白了:这意味着 iOS 7 & 8
    • 令人难以置信的是,XCode 会自动为这些图像资产创建一个文件名,并让您跳过箍来弄清楚如何直接访问它们......
    【解决方案3】:

    根据@Pichirich 的回答,我在 InterfaceBuilder 中引用了我的启动图像:

    “LaunchImage.png”

    ...在 Xcode 5.0.2 中,它会自动从资产目录中直接提取适当的图像。

    这正是我所期望的——除了 Apple 将“Default.png”静默重命名为“LaunchImage.png”的恶毒举动:)

    【讨论】:

    • 还有一点需要注意。这些图像的大小应该与 Apple 推荐的完全一样(例如 iOS 5-6 iPhone 3GS 的 LaunchImage 为 320x480),否则在给定初始化后它将是 nil
    【解决方案4】:

    documentation中有明确说明:

    “资产目录中的每个集合都有一个名称您可以使用该名称以编程方式加载集合中包含的任何单个图像。要加载图像,请调用UIImage:ImageNamed: 方法,传递包含图像的集合的名称。”

    使用 Pichirich 的列表有助于解决这种不一致问题。

    【讨论】:

    • 请注意“集合名称”部分。查看我的资产目录,我有一个名为“LaunchImage”的集合。然后,为了加载启动图像,我调用了:UIImageView *myView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"LaunchImage"]]; 效果很好!
    • 无需使用 Pichirich 的列表(认为这仍然是很棒的信息) - 只需使用资产目录“集合”的名称即可。
    • 好吧,对我来说,这不适用于 Xcode 6.0.1 和 iOS 8 上的启动图像。LaunchImage 似乎很特别,因为图像最终单独出现在编译的应用程序包中并且不会保留在 xcasset 捆绑文件夹中。
    • 如果有两个不同的资产目录包含同名集,会发生什么? [UIImage imageNamed:..] 怎么知道该选哪一个?
    • 对我来说这行不通,XCode 6.0.1 iOS 7 iPod Touch
    【解决方案5】:

    我刚刚编写了一个通用方法来获取 iPhone 和 iPad 的启动图像名称(横向、纵向),它对我有用,希望它也能帮助你。我在其他 SO 答案的帮助下写了这篇文章,感谢@Pichirichi 提供整个列表。

    +(NSString*)getLaunchImageName
    {
    
     NSArray* images= @[@"LaunchImage.png", @"LaunchImage@2x.png",@"LaunchImage-700@2x.png",@"LaunchImage-568h@2x.png",@"LaunchImage-700-568h@2x.png",@"LaunchImage-700-Portrait@2x~ipad.png",@"LaunchImage-Portrait@2x~ipad.png",@"LaunchImage-700-Portrait~ipad.png",@"LaunchImage-Portrait~ipad.png",@"LaunchImage-Landscape@2x~ipad.png",@"LaunchImage-700-Landscape@2x~ipad.png",@"LaunchImage-Landscape~ipad.png",@"LaunchImage-700-Landscape~ipad.png"];
    
    UIImage *splashImage;
    
    if ([self isDeviceiPhone])
    {
        if ([self isDeviceiPhone4] && [self isDeviceRetina])
        {
            splashImage = [UIImage imageNamed:images[1]];
            if (splashImage.size.width!=0)
                return images[1];
            else
                return images[2];
        }
        else if ([self isDeviceiPhone5])
        {
            splashImage = [UIImage imageNamed:images[1]];
            if (splashImage.size.width!=0)
                return images[3];
            else
                return images[4];
        }
        else
            return images[0]; //Non-retina iPhone
    }
    else if ([[UIDevice currentDevice] orientation]==UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown)//iPad Portrait
    {
        if ([self isDeviceRetina])
        {
            splashImage = [UIImage imageNamed:images[5]];
            if (splashImage.size.width!=0)
                return images[5];
            else
                return images[6];
        }
        else
        {
            splashImage = [UIImage imageNamed:images[7]];
            if (splashImage.size.width!=0)
                return images[7];
            else
                return images[8];
        }
    
    }
    else
    {
        if ([self isDeviceRetina])
        {
            splashImage = [UIImage imageNamed:images[9]];
            if (splashImage.size.width!=0)
                return images[9];
            else
                return images[10];
        }
        else
        {
            splashImage = [UIImage imageNamed:images[11]];
            if (splashImage.size.width!=0)
                return images[11];
            else
                return images[12];
        }
     }
    }
    

    其他实用方法是

    +(BOOL)isDeviceiPhone
    {
     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
     {
         return TRUE;
     }
    
     return FALSE;
    }
    
    +(BOOL)isDeviceiPhone4
    {
     if ([[UIScreen mainScreen] bounds].size.height==480)
        return TRUE;
    
     return FALSE;
    }
    
    
    +(BOOL)isDeviceRetina
    {
     if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] &&
        ([UIScreen mainScreen].scale == 2.0))        // Retina display
     {
        return TRUE;
     } 
     else                                          // non-Retina display
     {
         return FALSE;
     }
    }
    
    
    +(BOOL)isDeviceiPhone5
    {
     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && [[UIScreen mainScreen] bounds].size.height>480)
     {
        return TRUE;
     }
     return FALSE;
    }
    

    【讨论】:

    • isDeviceiPhone4 的这段代码实际上有一个小错误:[[UIScreen mainScreen] bounds] 现在会根据您在 iOS 8 下所处的方向而变化。您需要将其显式转换为纵向bounds 做类似的事情:[screen.coordinateSpace convertRect:screen.bounds toCoordinateSpace:screen.fixedCoordinateSpace],但请务必先测试您是否在 iOS 8 上,否则会崩溃。
    • 感谢@Hafez 指出这一点,我将在 iOS 8 上对其进行测试并尽快更新答案。
    【解决方案6】:

    我意识到这不一定是每个人的最佳解决方案,但最简单(且最不容易出错,恕我直言)的方法是在您的 Images.xcassets 目录中创建一个单独的条目。我称它为SplashImage

    当您添加新条目时,请确保不要选择“新启动图像”作为选项。相反,选择通用的“新图像集”。接下来,打开检查器并选择相关选项。如果您像我一样只为视网膜设备构建,您可以选择以下内容:

    这将为您留下四个条目(iPhone 4S、iPhone 5(s,c)、iPhone 6 和 iPhone 6 Plus)。

    图片对应的文件如下:

    | Resolution (Xcode entry) | Launch Image name   |   Device         |
    |--------------------------|---------------------|------------------|
    | 1x                       | Default-750.png     | iPhone 6         |
    | 2x                       | Default@2x.png      | iPhone 4S        |
    | Retina 4 2x              | Default-568h@2x.png | iPhone 5, 5s, 5c |
    | 3x                       | Default-1242.png    | iPhone 6 Plus    |
    

    当然,完成此操作后,您可以简单地使用[UIImage imageNamed:@"SplashImage"]

    【讨论】:

    • 有趣的想法,但它在 iPhone 6 上不起作用。它仍然在 iPhone 6 模拟器上加载 Default@2x.png 图像。
    • 使用这种方法,您还应该注意横向的启动图像集。
    【解决方案7】:

    我的应用目前仅支持 iOS 7 及更高版本。

    这是我从资产目录中引用启动图像的方式:

    NSDictionary *dict = @{@"320x480" : @"LaunchImage-700",
                           @"320x568" : @"LaunchImage-700-568h",
                           @"375x667" : @"LaunchImage-800-667h",
                           @"414x736" : @"LaunchImage-800-Portrait-736h"};
    NSString *key = [NSString stringWithFormat:@"%dx%d",
        (int)[UIScreen mainScreen].bounds.size.width,
        (int)[UIScreen mainScreen].bounds.size.height];
    UIImage *launchImage = [UIImage imageNamed:dict[key]];
    

    如果您想支持旧 iOS 版本,可以添加更多键值对。

    【讨论】:

    • 请注意,从 iOS 8 开始,UIScreen.mainScreen.bounds 会根据当前界面方向而有所不同。见stackoverflow.com/a/24153540/158525
    • 谢谢你,正是我想要的!
    • 感谢htis,有什么方法可以访问应用图标吗?
    【解决方案8】:

    只需一行代码即可轻松访问 Launch 图片。

     UIImage *myAppsLaunchImage = [UIImage launchImage];
    

    请按照下面给出的步骤来实现上述功能。

    第 1 步。 通过创建类别扩展 UIImage 类并向其添加以下方法。

    + (UIImage *)launchImage {
        NSDictionary *dOfLaunchImage = [NSDictionary dictionaryWithObjectsAndKeys:
                                        @"LaunchImage-568h@2x.png",@"568,320,2,8,p", // ios 8 - iphone 5 - portrait
                                        @"LaunchImage-568h@2x.png",@"568,320,2,8,l", // ios 8 - iphone 5 - landscape
                                        @"LaunchImage-700-568h@2x.png",@"568,320,2,7,p", // ios 7 - iphone 5 - portrait
                                        @"LaunchImage-700-568h@2x.png",@"568,320,2,7,l", // ios 7 - iphone 5 - landscape
                                        @"LaunchImage-700-Landscape@2x~ipad.png",@"1024,768,2,7,l", // ios 7 - ipad retina - landscape
                                        @"LaunchImage-700-Landscape~ipad.png",@"1024,768,1,7,l", // ios 7 - ipad regular - landscape
                                        @"LaunchImage-700-Portrait@2x~ipad.png",@"1024,768,2,7,p", // ios 7 - ipad retina - portrait
                                        @"LaunchImage-700-Portrait~ipad.png",@"1024,768,1,7,p", // ios 7 - ipad regular - portrait
                                        @"LaunchImage-700@2x.png",@"480,320,2,7,p", // ios 7 - iphone 4/4s retina - portrait
                                        @"LaunchImage-700@2x.png",@"480,320,2,7,l", // ios 7 - iphone 4/4s retina - landscape
                                        @"LaunchImage-Landscape@2x~ipad.png",@"1024,768,2,8,l", // ios 8 - ipad retina - landscape
                                        @"LaunchImage-Landscape~ipad.png",@"1024,768,1,8,l", // ios 8 - ipad regular - landscape
                                        @"LaunchImage-Portrait@2x~ipad.png",@"1024,768,2,8,p", // ios 8 - ipad retina - portrait
                                        @"LaunchImage-Portrait~ipad.png",@"1024,768,1,8,l", // ios 8 - ipad regular - portrait
                                        @"LaunchImage.png",@"480,320,1,7,p", // ios 6 - iphone 3g/3gs - portrait
                                        @"LaunchImage.png",@"480,320,1,7,l", // ios 6 - iphone 3g/3gs - landscape
                                        @"LaunchImage@2x.png",@"480,320,2,8,p", // ios 6,7,8 - iphone 4/4s - portrait
                                        @"LaunchImage@2x.png",@"480,320,2,8,l", // ios 6,7,8 - iphone 4/4s - landscape
                                        @"LaunchImage-800-667h@2x.png",@"667,375,2,8,p", // ios 8 - iphone 6 - portrait
                                        @"LaunchImage-800-667h@2x.png",@"667,375,2,8,l", // ios 8 - iphone 6 - landscape
                                        @"LaunchImage-800-Portrait-736h@3x.png",@"736,414,3,8,p", // ios 8 - iphone 6 plus - portrait
                                        @"LaunchImage-800-Landscape-736h@3x.png",@"736,414,3,8,l", // ios 8 - iphone 6 plus - landscape
                                        nil];
        NSInteger width = ([UIScreen mainScreen].bounds.size.width>[UIScreen mainScreen].bounds.size.height)?[UIScreen mainScreen].bounds.size.width:[UIScreen mainScreen].bounds.size.height;
        NSInteger height = ([UIScreen mainScreen].bounds.size.width>[UIScreen mainScreen].bounds.size.height)?[UIScreen mainScreen].bounds.size.height:[UIScreen mainScreen].bounds.size.width;
        NSInteger os = [[[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."] objectAtIndex:0] integerValue];
        NSString *strOrientation = UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation])?@"l":@"p";
        NSString *strImageName = [NSString stringWithFormat:@"%li,%li,%li,%li,%@",width,height,(NSInteger)[UIScreen mainScreen].scale,os,strOrientation];
        UIImage *imageToReturn = [UIImage imageNamed:[dOfLaunchImage valueForKey:strImageName]];
        if([strOrientation isEqualToString:@"l"] && [strImageName rangeOfString:@"Landscape"].length==0) {
            imageToReturn = [UIImage rotate:imageToReturn orientation:UIImageOrientationRight];
        }
        return imageToReturn;
    }
    

    第 2 步。 通过将以下代码也添加到 UIImage 的同一类别中,上述方法应该可以工作

    static inline double radians (double degrees) {return degrees * M_PI/180;}
    
    + (UIImage *)rotate:(UIImage*)src orientation:(UIImageOrientation) orientation {
        UIGraphicsBeginImageContext(src.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        if (orientation == UIImageOrientationRight) {
            CGContextRotateCTM (context, radians(90));
        } else if (orientation == UIImageOrientationLeft) {
            CGContextRotateCTM (context, radians(-90));
        } else if (orientation == UIImageOrientationDown) {
            // NOTHING
        } else if (orientation == UIImageOrientationUp) {
            CGContextRotateCTM (context, radians(90));
        }
        [src drawAtPoint:CGPointMake(0, 0)];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    

    【讨论】:

    • 很好的答案,谢谢!
    • 现在 iPhone X 启动图片的名称是什么?
    【解决方案9】:

    在 Pichirich 的回答的帮助下,我实现了以下类别(iOS 7+): UIImage+AssetLaunchImage

    它实际上只是动态生成名称,但可能会有所帮助。

    【讨论】:

      【解决方案10】:
      - (NSString *)splashImageNameForOrientation:(UIInterfaceOrientation)orientation {
          CGSize viewSize = self.view.bounds.size;
          NSString* viewOrientation = @"Portrait";
          if (UIDeviceOrientationIsLandscape(orientation)) {
              viewSize = CGSizeMake(viewSize.height, viewSize.width);
              viewOrientation = @"Landscape";
          }
      
          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"]])
                  return dict[@"UILaunchImageName"];
          }
          return nil;
      }
      

      【讨论】:

      • 效果很好。巧妙而优雅的方法来搜索主包的信息字典以查找可用的启动图像,然后选择具有匹配分辨率的图像!
      • 这是一个绝妙的主意,比我的要好,也是未来的证明,除非 Apple 改变 info.plist 的结构。
      • 这是一个非常聪明的解决方案。我的 Xcode 项目中有多个目标,仅使用 LaunchImage 字符串并不总是返回正确的图像。非常感谢。
      • 好主意。但不适用于状态栏不透明的屏幕。所以需要将 self.view.bounds.size 改为 [UIScreen mainScreen].bounds.size
      • 很好的解决方案。需要进行少量编辑:从 UIInterfaceOrientation 到 UIDeviceOrientation 的隐式转换。请改用UIInterfaceOrientationIsLandscape()
      【解决方案11】:

      这里是基于上面Cherpak Evgeny提供的解决方案的UIImage上的一个类别。

      UIImage+SplashImage.h

      #import <UIKit/UIKit.h>
      
      /**
       * Category on `UIImage` to access the splash image.
       **/
      @interface UIImage (SplashImage)
      
      /**
       * Return the name of the splash image for a given orientation.
       * @param orientation The interface orientation.
       * @return The name of the splash image.
       **/
      + (NSString *)si_splashImageNameForOrientation:(UIInterfaceOrientation)orientation;
      
      /**
       * Returns the splash image for a given orientation.
       * @param orientation The interface orientation.
       * @return The splash image.
       **/
      + (UIImage*)si_splashImageForOrientation:(UIInterfaceOrientation)orientation;
      
      @end
      

      UIImage+SplashImage.m

      #import "UIImage+SplashImage.h"
      
      @implementation UIImage (SplashImage)
      
      + (NSString *)si_splashImageNameForOrientation:(UIInterfaceOrientation)orientation
      {
          CGSize viewSize = [UIScreen mainScreen].bounds.size;
      
          NSString *viewOrientation = @"Portrait";
      
          if (UIDeviceOrientationIsLandscape(orientation))
          {
              viewSize = CGSizeMake(viewSize.height, viewSize.width);
              viewOrientation = @"Landscape";
          }
      
          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"]])
                  return dict[@"UILaunchImageName"];
          }
          return nil;
      }
      
      + (UIImage*)si_splashImageForOrientation:(UIInterfaceOrientation)orientation
      {
          NSString *imageName = [self si_splashImageNameForOrientation:orientation];
          UIImage *image = [UIImage imageNamed:imageName];
          return image;
      }
      
      @end
      

      【讨论】:

      • imageNamed 将图像推送到系统缓存,但启动图像有时非常巨大,所以它在内存中直到缓存刷新它
      【解决方案12】:

      Cherpak Evgeny 答案的 Swift 版本:

          func splashImageForOrientation(orientation: UIInterfaceOrientation) -> String {
              var viewSize = self.view.bounds.size
              var viewOrientation = "Portrait"
              if UIInterfaceOrientationIsLandscape(orientation) {
                 viewSize = CGSizeMake(viewSize.height, viewSize.width)
                 viewOrientation = "Landscape"
              }
              let imagesDict = NSBundle.mainBundle().infoDictionary as Dictionary<NSObject,AnyObject>!
              let imagesArray = imagesDict["UILaunchImages"] as NSArray
              for dict in imagesArray {
                  let dictNSDict = dict as NSDictionary
                  let imageSize = CGSizeFromString(dictNSDict["UILaunchImageSize"] as String)
                  if CGSizeEqualToSize(imageSize, viewSize) && viewOrientation == (dictNSDict["UILaunchImageOrientation"] as String) {
                      return dictNSDict["UILaunchImageName"] as String
                  }
              }
              return ""
          }
      

      【讨论】:

        【解决方案13】:

        @codeman 为 Swift 1.2 更新了答案:

        func splashImageForOrientation(orientation: UIInterfaceOrientation, size: CGSize) -> String? {
            var viewSize        = size
            var viewOrientation = "Portrait"
        
            if UIInterfaceOrientationIsLandscape(orientation) {
                viewSize        = CGSizeMake(size.height, size.width)
                viewOrientation = "Landscape"
            }
        
            if let imagesDict = NSBundle.mainBundle().infoDictionary as? [String: AnyObject] {
                if let imagesArray = imagesDict["UILaunchImages"] as? [[String: String]] {
                    for dict in imagesArray {
                        if let sizeString = dict["UILaunchImageSize"], let imageOrientation = dict["UILaunchImageOrientation"] {
                            let imageSize = CGSizeFromString(sizeString)
                            if CGSizeEqualToSize(imageSize, viewSize) && viewOrientation == imageOrientation {
                                if let imageName = dict["UILaunchImageName"] {
                                    return imageName
                                }
                            }
                        }
                    }
                }
            }
        
            return nil
        
        }
        

        调用它并支持 iOS 8 的旋转:

        override func viewWillAppear(animated: Bool) {
            if let img = splashImageForOrientation(UIApplication.sharedApplication().statusBarOrientation, size: self.view.bounds.size) {
                backgroundImage.image = UIImage(named: img)
            }
        }
        
        override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
            let orientation = size.height > size.width ? UIInterfaceOrientation.Portrait : UIInterfaceOrientation.LandscapeLeft
        
            if let img = splashImageForOrientation(orientation, size: size) {
                backgroundImage.image = UIImage(named: img)
            }
        
        }
        

        正是我需要的,谢谢!

        【讨论】:

          【解决方案14】:

          更新到最新的 Swift 语法 (Swift 5)

             func splashImageForOrientation(orientation: UIInterfaceOrientation) -> String? {
          
              var viewSize = screenSize
              var viewOrientation = "Portrait"
              if orientation.isLandscape {
                  viewSize = CGSize(width: viewSize.height, height: viewSize.width)
                  viewOrientation = "Landscape"
              }
              if let infoDict = Bundle.main.infoDictionary, let launchImagesArray = infoDict["UILaunchImages"] as? [Any] {
                  for launchImage in launchImagesArray {
                      if let launchImage = launchImage as? [String: Any], let nameString = launchImage["UILaunchImageName"] as? String, let sizeString = launchImage["UILaunchImageSize"] as? String, let orientationString = launchImage["UILaunchImageOrientation"] as? String {
                          let imageSize = NSCoder.cgSize(for: sizeString)
                          if imageSize.equalTo(viewSize) && viewOrientation == orientationString {
                              return nameString
                          }
                      }
                  }
              }
              return nil
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-03-03
            • 2014-03-12
            • 1970-01-01
            • 1970-01-01
            • 2014-01-07
            • 2018-08-27
            • 2014-02-21
            相关资源
            最近更新 更多