ios – 以编程方式访问iPad发布图像?

我在
Images.xcassets中使用LaunchImage.launchimage来管理启动图像.但我也试图在应用程序内部使用启动图像.

我读过this answer

The documentation indicates that the imageNamed: method on UIImage
should auto-magically select the correct version

所以,我使用了这段代码

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

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

那么如何以编程方式访问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"
  }
}

最佳答案 我用一个很长的方法解决了这个问题

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"];
        }
    }
}
点赞