4

我正在使用LaunchImage.launchimageinImages.xcassets来管理启动图像。但我也在尝试使用应用程序内的启动图像。

我读过这个答案

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

所以,我使用了这段代码

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

在 iPhone 4、5、6、6+ 上工作时,它工作得非常好并获得正确的 LaunchImage,但在iPad 视网膜上工作时,它返回LaunchImage-700@2x.png的是iPhone 4s的 LaunchImage 640 x 960 pixels

那么如何以编程方式访问 iPad 的正确 LaunchImage 呢?

文件夹内Contents.json的内容LaunchImage

{
  "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"
  }
}
4

3 回答 3

2

我用很长的方法解决了这个问题

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"];
        }
    }
}
于 2015-08-12T18:59:05.830 回答
0

我相信那些扩展名(例如-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"];
于 2015-08-09T08:56:18.807 回答
0

LaunchImage.launchimage

您真的在使用Images.xcassets管理启动图像吗?你不LaunchImage.launchimage改用吗?

如果是这样,就会出现混乱。您无法使用[UIImage imageNamed:@"LaunchImage"];from加载LaunchImage.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

于 2015-08-09T09:20:14.070 回答