简体   繁体   English

iOS:使用LaunchImage作为应用程序背景

[英]iOS: Using LaunchImage as app background

Is this possible without glitches? 没有毛刺,这有可能吗?

My Xcode project has a launch image defined in the assets (aka Images.xcassets) incl. 我的Xcode项目在资产(aka Images.xcassets)incl中定义了启动映像。 all supported sizes and orientations. 所有受支持的尺寸和方向。 Since I want to use the same image as background in my initial view, I add it in the view controller as background: 由于我想在初始视图中使用与背景相同的图像,因此将其添加到视图控制器中作为背景:

    view.layer.contents = UIImage(named:"LaunchImage").CGImage;

However when the launch screen switches to the initial view there's a noticeable glitch since the background image set in the view stretches. 但是,当启动屏幕切换到初始视图时,由于视图中设置的背景图像会拉伸,因此会出现明显的故障。 Also when I rotate the device, the image stretches. 同样,当我旋转设备时,图像会拉伸。

The stretching occurring when the device is rotated seems plausible since obviously the other image sizes/orientations in the asset catalog image aren't regarded when set as a background image (my guess). 旋转设备时发生的拉伸似乎是合理的,因为当设置为背景图像时,资产目录图像中的其他图像尺寸/方向显然不被考虑(我的猜测)。

But the stretching that occurs between launch screen and my initial view seems rather strange since the two should have the same size. 但是在启动屏幕和我的初始视图之间发生的拉伸似乎很奇怪,因为两者应该具有相同的大小。

Is there any tried and tested procedure to have the bg image always adapt with aspect ratio in mind when the device orientation changes? 当设备方向发生变化时,是否有任何经过实践检验的程序可以使bg图像始终适应宽高比? And is this even possible wit the (multi-sized) launch image? 而且,(多尺寸)启动图像甚至可能吗?


UPDATE: 更新:

One possible method to add a bg image is by placing a UIImageView in the background (behind all other views) and load the image into it. 添加背景图像的一种可能方法是将UIImageView放在背景中(在所有其他视图的后面),然后将图像加载到其中。 That solution takes care of the device orientation issue and would be OK for me but there is another issue: It loads the wrong image size. 该解决方案可以解决设备方向问题,对我来说可以,但是还有另一个问题:加载错误的图像尺寸。 If I test it on iPad2 emu it should use the image named Default~ipad~nostatusbar.png but if I check the size of the UIImage it is only 320x480 pixels so it obviously used the image named Default.png . 如果我在iPad2上进行测试,它应该使用名为Default〜ipad〜nostatusbar.png的图像,但是如果我检查UIImage的大小,则只有320x480像素,因此显然使用了名为Default.png的图像。

And I can only load the launch image by the name defined in the Images.xcassets. 而且我只能按Images.xcassets中定义的名称加载启动图像。 If I try to create a UIImage with physical filename (eg Default.png) the bg will stay black. 如果我尝试使用物理文件名(例如Default.png)创建UIImage,则bg将保持黑色。

So the question is: How do I get iOS to choose the right image size/orientation from LaunchImage in Images.xcassets? 所以问题是:如何让iOS从Images.xcassets中的LaunchImage中选择正确的图像大小/方向?

To fully resolve this issue, here's my Swift version of Daij-Djan's code. 为了完全解决此问题,这是我的Daij-Djan代码的Swift版本。 It also lists the ridiculous amount of different launch image versions in the comment block. 它还在注释块中列出了不同数量的启动映像版本。 there are some calls to my own util lib (EnvUtil) but that's another story and most users can figure out how to get the needed vars. 有一些对我自己的util lib(EnvUtil)的调用,但这是另一个故事,大多数用户可以弄清楚如何获取所需的var。

/**
   Returns the default image (launch image) of the application with regard to the screen size and orientation.

   Default.png                               | LaunchImage.png                       |  320 x 480  | iPhone 4 Low Res.
   Default@2x.png                            | LaunchImage@2x.png                    |  640 x 960  | iPhone 4
   Default@2x.png                            | LaunchImage-700@2x.png                |  640 x 960  | iPhone 4 iOS7
   Default-568h@2x.png                       | LaunchImage-568h@2x.png               |  640 x 1136 | iPhone 5
   Default-568h@2x.png                       | LaunchImage-700-568h@2x.png           |  640 x 1136 | iPhone 5
   Default~ipad.png                          | LaunchImage-Portrait~ipad.png         |  768 x 1004 | iPad Portrait
   Default~ipad~nostatusbar.png              | LaunchImage-700-Portrait~ipad.png     |  768 x 1024 | iPad Portrait FS
   Default~ipad~landscape.png                | LaunchImage-Landscape~ipad.png        | 1024 x 748  | iPad Landscape
   Default~ipad~landscape~nostatusbar.png    | LaunchImage-700-Landscape~ipad.png    | 1024 x 768  | iPad Landscape FS
   Default~ipad@2x.png                       | LaunchImage-Portrait@2x~ipad.png      | 1536 x 2008 | iPad Portrait Retina
   Default~ipad~nostatusbar@2x.png           | LaunchImage-700-Portrait@2x~ipad.png  | 1536 x 2048 | iPad Portrait Retina FS
   Default~ipad~landscape@2x.png             | LaunchImage-Landscape@2x~ipad.png     | 2048 x 1496 | iPad Landscape Retina
   Default~ipad~landscape~nostatusbar@2x.png | LaunchImage-700-Landscape@2x~ipad.png | 2048 x 1536 | iPad Landscape Retina FS
 */
public class func getDefaultImage() -> UIImage
{
    var fileName:String = "LaunchImage";
    let osVersion:String = EnvUtil.systemVersionMajor() > 6 ? "700" : "";
    let scale:String = EnvUtil.isRetina() ? "@2x" : "";
    let div:String = osVersion.length > 0 ? "-" : "";

    if (EnvUtil.isPad())
    {
        let orientation:String = EnvUtil.isPortraitOrientation() ? "Portrait" : "Landscape";
        fileName += "-" + osVersion + div + orientation + scale + "~ipad";
    }
    else
    {
        if (CGRectGetHeight(UIScreen.mainScreen().bounds) > 480.0)
        {
            /* iPhone 5. */
            fileName += "-" + osVersion + div + "568h@2x";
        }
        else
        {
            /* iPhone 4 with iOS7? */
            fileName += div + osVersion + scale;
        }
    }

    fileName += ".png";
    //Log.debug(fileName);

    return UIImage(named: fileName);
}

I have a UIImage+DefaultImage [ios] category in objC that allows this. 我在objC中有一个UIImage + DefaultImage [ios]类别,它允许这样做。 Should be easy to port to a swift extension 应该易于移植到快速扩展

#import <UIKit/UIKit.h>

@interface UIImage (DefaultImage)

// uses statusbar orientation
+ (UIImage *)defaultImage;

//uses given orientation
+ (UIImage *)defaultImageForOrientation:(UIInterfaceOrientation)orient;

@end

@implementation UIImage (DefaultImage)

+ (UIImage *)defaultImage {
    return [self defaultImageForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
}

+ (UIImage  *)defaultImageForOrientation:(UIInterfaceOrientation)orient {
    // choose the correct launch image for orientation, device and scale
    NSMutableString *launchImageName = [[NSMutableString alloc] initWithString:@"Default"];
    BOOL isPad = ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad );
    if ( isPad ) {
        BOOL isLandscape = UIInterfaceOrientationIsLandscape(orient);
        NSString *imageOrientation = (isLandscape) ? @"Landscape" : @"Portrait";

        BOOL isRetina = ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2.0);
        NSString *scaleString = (isRetina) ? @"@2x" : @"";

        // Default-Landscape~ipad.png
        // Default-Landscape@2x~ipad.png
        // Default-Portrait~ipad.png
        // Default-Portrait@2x~ipad.png
        launchImageName = [NSMutableString stringWithFormat:@"%@-%@%@.png", launchImageName, imageOrientation, scaleString];       
    } else {
        if ( CGRectGetHeight([UIScreen mainScreen].bounds) > 480.f) {
            // Default-568h.png
            launchImageName = [NSMutableString stringWithFormat:@"%@-568h.png", launchImageName];
        } else {
            // Default.png
            // Default@2x.png
            launchImageName = [NSMutableString stringWithFormat:@"%@.png", launchImageName];
        }
    }
    return [UIImage imageNamed:launchImageName];
}

@end

Disclaimer: my own code -- taken from https://github.com/Daij-Djan/DDUtils 免责声明:我自己的代码-来自https://github.com/Daij-Djan/DDUtils

启动图像位于应用程序捆绑包中(命名为default ..),因此您只需要将其加载到UIImageView中并将该imageView添加到UIView中即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM