ios - 以编程方式访问 iPad 启动图像?

标签 ios objective-c ipad cocoa-touch launchimage

我在 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 retina 上工作时,它返回 LaunchImage-700@2x.png 这是 iPhone 4s 的 LaunchImage 640 x 960 像素

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

关于ios - 以编程方式访问 iPad 启动图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31901678/

相关文章:

ios - 如何使用 objective-c 在iphone模拟器中显示图像

ios - 如何为 UIButton 设置默认状态图像?

iphone - iOS5 上的 CCKeyDerivationPBKDF

objective-c - 理解 Objective-C 中的实例化

objective-c - AFNetworking 和 UIWebView 之间的共享 session

iphone - 将注释和 Pdf 保存到 IOS 5 中的新 Pdf 文件

Swift 4 相机 View 在 iPad 上出现在侧面

iOS:在 uiimage 上具有发光/阴影效果的羽毛

javascript - 防止父项默认触摸移动,但子项不默认

ios - Swift – 表格 View reloadData 在编辑单元格内容后不起作用