ios - Xcode 7, Assets 目录通用设备背景图片支持吗?

标签 ios iphone xcode xcode7 xcasset

我看过各种关于图像尺寸的旧帖子,但我找不到任何最新信息,甚至不知道是否可以仅使用 Assets 目录来提供适用于所有 iPad 和 iPhone 屏幕尺寸的图像。?

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

Xcode 6 - xcassets for universal image support

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

我看到了您可以在 Assets 目录之外提供自己的图像的选项,但我真的很想使用 Assets 目录。

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

编辑: 看起来我可能不得不选择 none asset catalog 路线 :(

一个)

我想对这个解决方案进行 future 验证,所以它回退,如果需要,调整最合适的图像大小,我不确定是否会发生这种情况。

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 目录中:)

要为 iPhone 和 iPad 提供不同的图像,您只需选中“设备”部分下的 iPhone 和 iPad 复选框并取消选中“通用”。

但是我仍然不知道如何处理 iPhone 6。我总是为 iPhone 6 设置不同的图像,只有一张图像,如果设备是 iPhone6 或 iPhone6 模拟器,我会检查代码并设置该图像.

关于ios - Xcode 7, Assets 目录通用设备背景图片支持吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32948931/

相关文章:

ios - 检测 iOS 10.3 应用评级对话框显示的机制?

ios - AFAmazonS3Manager 可接受的图像内容类型

ios - AVPlayer 有时无法播放视频

iphone - 配置门户上开发和分发配置文件的区别?

iOS 11 使用视觉框架 VNDetectRectanglesRequest 做对象检测不准确?

ios - 如何发送垃圾 '&' 作为 url 参数 - swift

iphone - AVMutableAudioMix 和 AVAssetExportSession

ios - Xcode 的 Profiler 不显示符号名称

ios - Xcode 调试检查器将值显示为 nil

ios - 双击需要用搜索栏选择 TableView 项目