【问题标题】:Access iPad launch images programmatically?以编程方式访问 iPad 启动图像?
【发布时间】:2015-11-01 07:29:26
【问题描述】:

我在Images.xcassets 中使用LaunchImage.launchimage 来管理启动图像。但我也在尝试使用应用内的启动图像。

我读过this answer

文档表明 UIImage 上的 imageNamed: 方法 应该自动地选择正确的版本

所以,我使用了这个代码

UIImageView *backImage = [UIImageView new];
backImage.image = [UIImage imageNamed:@"LaunchImage"];

在 iPhone 4、5、6、6+ 上工作时,它运行良好并获得正确的 LaunchImage,但在 iPad Retina 上工作时,它返回 LaunchImage-700@2x.png,这是 iPhone 4s 640 x 960 pixels

那么我怎样才能以编程方式访问 iPad 的正确 LaunchImage 呢??

LaunchImage文件夹中Contents.json的内容

{
  "images" : [
    {
      "extent" : "full-screen",
      "idiom" : "iphone",
      "subtype" : "736h",
      "filename" : "LaunchImage-800-Portrait-736h@3x.png",
      "minimum-system-version" : "8.0",
      "orientation" : "portrait",
      "scale" : "3x"
    },
    {
      "extent" : "full-screen",
      "idiom" : "iphone",
      "subtype" : "667h",
      "filename" : "LaunchImage-800-667h@2x.png",
      "minimum-system-version" : "8.0",
      "orientation" : "portrait",
      "scale" : "2x"
    },
    {
      "orientation" : "portrait",
      "idiom" : "iphone",
      "extent" : "full-screen",
      "minimum-system-version" : "7.0",
      "filename" : "LaunchImage-700@2x.png",
      "scale" : "2x"
    },
    {
      "extent" : "full-screen",
      "idiom" : "iphone",
      "subtype" : "retina4",
      "filename" : "LaunchImage-700-568h@2x.png",
      "minimum-system-version" : "7.0",
      "orientation" : "portrait",
      "scale" : "2x"
    },
    {
      "orientation" : "portrait",
      "idiom" : "ipad",
      "extent" : "full-screen",
      "minimum-system-version" : "7.0",
      "filename" : "LaunchImage-700-Portrait~ipad.png",
      "scale" : "1x"
    },
    {
      "orientation" : "landscape",
      "idiom" : "ipad",
      "extent" : "full-screen",
      "minimum-system-version" : "7.0",
      "filename" : "LaunchImage-700-Landscape~ipad.png",
      "scale" : "1x"
    },
    {
      "orientation" : "portrait",
      "idiom" : "ipad",
      "extent" : "full-screen",
      "minimum-system-version" : "7.0",
      "filename" : "LaunchImage-700-Portrait@2x~ipad.png",
      "scale" : "2x"
    },
    {
      "orientation" : "landscape",
      "idiom" : "ipad",
      "extent" : "full-screen",
      "minimum-system-version" : "7.0",
      "filename" : "LaunchImage-700-Landscape@2x~ipad.png",
      "scale" : "2x"
    },
    {
      "orientation" : "portrait",
      "idiom" : "iphone",
      "extent" : "full-screen",
      "filename" : "LaunchImage-700.png",
      "scale" : "1x"
    },
    {
      "orientation" : "portrait",
      "idiom" : "iphone",
      "extent" : "full-screen",
      "filename" : "LaunchImage-700@2x.png",
      "scale" : "2x"
    },
    {
      "orientation" : "portrait",
      "idiom" : "iphone",
      "extent" : "full-screen",
      "filename" : "LaunchImage-700-568h@2x.png",
      "subtype" : "retina4",
      "scale" : "2x"
    },
    {
      "orientation" : "portrait",
      "idiom" : "ipad",
      "extent" : "full-screen",
      "filename" : "LaunchImage-700-Portrait~ipad.png",
      "scale" : "1x"
    },
    {
      "orientation" : "landscape",
      "idiom" : "ipad",
      "extent" : "full-screen",
      "filename" : "LaunchImage-700-Landscape~ipad.png",
      "scale" : "1x"
    },
    {
      "orientation" : "portrait",
      "idiom" : "ipad",
      "extent" : "full-screen",
      "filename" : "LaunchImage-700-Portrait@2x~ipad.png",
      "scale" : "2x"
    },
    {
      "orientation" : "landscape",
      "idiom" : "ipad",
      "extent" : "full-screen",
      "filename" : "LaunchImage-700-Landscape@2x~ipad.png",
      "scale" : "2x"
    }
  ],
  "info" : {
    "version" : 1,
    "author" : "xcode"
  }
}

【问题讨论】:

    标签: ios objective-c ipad cocoa-touch launchimage


    【解决方案1】:

    我认为这些扩展(例如 -700-Landscape@2x~ipad)无法识别。

    将以下项目添加到您的 Contents.json 中,仅用于测试目的:

    {
          "orientation" : "landscape",
          "idiom" : "ipad",
          "extent" : "full-screen",
          "filename" : "LaunchImage.png",
          "scale" : "1x"
    }
    

    现在试试 imageNamed: 方法。

    或者试试 imageNamed: 和图片全名:

    backImage.image = [UIImage imageNamed:@"LaunchImage-700-Landscape~ipad.png"];
    

    【讨论】:

    • 如果这不是你的。您需要获取 Images.xcassets 捆绑包。请确认。我将添加获取 Images.xcassets 包的代码。
    • 抱歉.. 将LaunchImage-700-Portrait~ipad.png 重命名为LaunchImage.png 并编辑Contents.json 后的事件,没有任何改变,仍然返回 iPhone 4s 图像
    • 但是当使用像 [UIImage imageNamed:@"LaunchImage-700-Landscape~ipad.png"] 这样的完整图像名称时,它可以工作..但这需要更多的代码才能正常工作
    • 我知道。那也对我有用。我相信苹果从来没有 100% 支持所有这些扩展。只有一些作品。简单的@2x 而已
    【解决方案2】:

    LaunchImage.launchimage

    您真的使用Images.xcassets 来管理启动图像吗? 你不是用LaunchImage.launchimage 代替吗?

    如果是这样,就会出现混乱。您无法使用来自LaunchImage.launchimage[UIImage imageNamed:@"LaunchImage"]; 加载。

    规范名称

    • LaunchImage.png
    • LaunchImage@2x.png
    • LaunchImage-700@2x.png
    • LaunchImage-568h@2x.png
    • LaunchImage-700-568h@2x.png
    • LaunchImage-700-Landscape@2x~ipad.png

    LaunchImage-700-肖像@2x~ipad.png不在列表中。

    【讨论】:

    • 是的,我使用的是LaunchImage.launchimage,它位于Images.xcassets
    • 即使在将 LaunchImage-700-Portrait~ipad.png 重命名为 LaunchImage-700~ipad.png 并编辑 Contents.json 之后,没有任何改变,仍然返回 iPhone 4s 启动图像
    【解决方案3】:

    我已经用一个长方法解决了这个问题

    BOOL deviceIsIpad = NO;
    if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
        deviceIsIpad = YES;
    }
    
    if (!deviceIsIpad) {
        backImage.image = [UIImage imageNamed:@"LaunchImage"];
    } else {
        UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;
        CGFloat screenScale = [[UIScreen mainScreen] scale];
        if (interfaceOrientation == UIDeviceOrientationPortrait || interfaceOrientation == UIDeviceOrientationPortraitUpsideDown){
            if (screenScale > 1) {
                backImage.image = [UIImage imageNamed:@"LaunchImage-700-Portrait@2x~ipad.png"];
            } else {
                backImage.image = [UIImage imageNamed:@"LaunchImage-700-Portrait~ipad.png"];
            }
        } else {
            if (screenScale > 1) {
                backImage.image = [UIImage imageNamed:@"LaunchImage-700-Landscape@2x~ipad.png"];
            } else {
                backImage.image = [UIImage imageNamed:@"LaunchImage-700-Landscape~ipad.png"];
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-10-24
      • 2015-08-15
      • 2012-03-03
      • 2011-01-31
      • 1970-01-01
      • 2020-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多