Xcode 7,资产目录通用设备背景图片支持吗? [英] Xcode 7, asset catalog universal device background image support?

查看:107
本文介绍了Xcode 7,资产目录通用设备背景图片支持吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过许多有关图像尺寸的旧文章,但是我找不到最新的信息,甚至无法确定仅通过资产目录就可以为所有iPad和iPhone屏幕尺寸提供图像. >

这是我找到的最好的帖子,但是在Xcode 7中,它不显示"Retina 4 2x"或iPhone 6/6 +

Xcode 6-用于通用图像支持的xcassets

在xcode 7中,有一个通用选项,但是三个图像并不支持所有设备尺寸.

我看到了可以在资产目录之外提供自己的图像的选项,但是我真的很想使用资产目录.

如何使用xcassets/通用背景不同iPhone/iPad的图片?

看来我可能不得不选择无资产目录路由:(

A)

我希望将来对该解决方案进行验证,因此它会退一步,如果需要,请调整最合适的图像大小,因为我不确定会发生这种情况.

NSNumber *screenWidth = @([UIScreen mainScreen].bounds.size.width);
NSString *imageName = [NSString stringWithFormat:@"name-%@w", screenWidth];
UIImage *image = [UIImage imageNamed:imageName];

B)

也许此代码更好?尽管我不确定这与什么尺寸有关,但由于它不支持x3图像,因此它也有些过时了?

#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

免责声明:摘自 https://github.com/Daij-Djan/DDUtils

C)

这看起来也不错,但是它正在调整大小,并且没有使用实际的清晰图像,也没有退缩.

https://gist.github.com/kevindelord/fe2e691d06ab745fbb00

NSString *extension = @"";      // iPhone 3GS and earlier
if (scale == 3.f) {
    extension = @"@3x";         // iPhone 6 Plus
} else if (scale == 2.f && h == 568.0f && w == 320.0f) {
    extension = @"-568h@2x";    // iPhone 5, 5S, 5C
} else if (scale == 2.f && h == 667.0f && w == 375.0f) {
    extension = @"-667h@2x";    // iPhone 6
} else if (scale == 2.f && h == 480.0f && w == 320.0f) {
    extension = @"@2x";         // iPhone 4, 4S
} else if (scale == 1.f && h == 1024.0f && w == 768.0f) {
    extension = @"-512h";       // iPad Mini, iPad 2, iPad 1
} else if (scale == 2.f && h == 1024.0f && w == 768.0f) {
    extension = @"-1024h@2x";   // iPad Mini 3, iPad Mini 2, iPad Air, iPad Air 2
}
return extension;

解决方案

我刚刚修改了Contents.json文件并添加了Retina 4 2x:

{
      "idiom" : "iphone",
      "filename" : "home-bgimage-iphone5@2x.png",
      "subtype" : "retina4",
      "scale" : "2x"
}

Retina 4 2x再次出现在该图像集的Assets Catalog中:)

要为iPhone和iPad提供不同的图像,您只需要选中设备"部分下的"iPhone和iPad"复选框,然后取消选择通用"复选框即可.

但是我仍然不知道如何处理iPhone6.我始终为iPhone 6设置了一个图像,但只设置了一个图像,并在代码中检查了该设备是否为iPhone6或iPhone6模拟器,而是设置了该图像.

I've seen various old posts regarding image sizes, but I can't find anything up-to-date or even know if it's possible with just asset catalog to provide images for all iPad and iPhone screen sizes.?

This is the best post I've found, but in Xcode 7 it doesn't show "Retina 4 2x" or the iPhone 6 / 6+

Xcode 6 - xcassets for universal image support

In xcode 7 there is a universal option, but the three images don't support all the device sizes.

I've seen options where you can provide your own images outside of asset catalog, but I'd really like to use asset catalog.

How to use xcassets/universal background images for different iPhones/iPads?

EDIT: It looks like I might have to go for the none asset catalog route :(

A)

I'd like to future proof this solution, so it falls back and if needs be, resize the most appropriate image, as-is I'm not sure will happen.

NSNumber *screenWidth = @([UIScreen mainScreen].bounds.size.width);
NSString *imageName = [NSString stringWithFormat:@"name-%@w", screenWidth];
UIImage *image = [UIImage imageNamed:imageName];

B)

Or maybe this code is better? Although I'm not sure what sizes this relates to, it's also a bit out of date as it doesn't support x3 images ?

#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: taken from https://github.com/Daij-Djan/DDUtils

C)

This also looks good, but it's re-sizing and not using actual sharp images and there's no fall back.

https://gist.github.com/kevindelord/fe2e691d06ab745fbb00

NSString *extension = @"";      // iPhone 3GS and earlier
if (scale == 3.f) {
    extension = @"@3x";         // iPhone 6 Plus
} else if (scale == 2.f && h == 568.0f && w == 320.0f) {
    extension = @"-568h@2x";    // iPhone 5, 5S, 5C
} else if (scale == 2.f && h == 667.0f && w == 375.0f) {
    extension = @"-667h@2x";    // iPhone 6
} else if (scale == 2.f && h == 480.0f && w == 320.0f) {
    extension = @"@2x";         // iPhone 4, 4S
} else if (scale == 1.f && h == 1024.0f && w == 768.0f) {
    extension = @"-512h";       // iPad Mini, iPad 2, iPad 1
} else if (scale == 2.f && h == 1024.0f && w == 768.0f) {
    extension = @"-1024h@2x";   // iPad Mini 3, iPad Mini 2, iPad Air, iPad Air 2
}
return extension;

解决方案

I just modified the Contents.json file and added the Retina 4 2x:

{
      "idiom" : "iphone",
      "filename" : "home-bgimage-iphone5@2x.png",
      "subtype" : "retina4",
      "scale" : "2x"
}

And the Retina 4 2x appeared back in Assets Catalog for that image set :)

To provide different images for iPhone and iPad you just need to select the iPhone and iPad check boxes under the Devices section and deselect the Universal one.

However I still don't know how to handle the iPhone 6. I always had a different image set for iPhone 6 with just one image and checked in the code if the device is an iPhone6 or iPhone6 Simulator and set that image instead.

这篇关于Xcode 7,资产目录通用设备背景图片支持吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆