ios – Xcode 7,资产目录通用设备背景图像支持?

我已经看过各种关于图像尺寸的旧帖子,但我找不到任何最新信息,甚至不知道是否可以通过资产目录为所有iPad和iPhone屏幕尺寸提供图像.

这是我发现的最好的帖子,但在Xcode 7中它没有显示“Retina 4 2x”或iPhone 6/6

Xcode 6 – xcassets for universal image support

在xcode 7中有一个通用选项,但这三个图像不支持所有设备大小.

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

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

编辑:
看起来我可能不得不去无资产目录路线:(

一个)

我希望将来证明这个解决方案,所以它会回落,如果需要,请调整最合适的图像,因为我不确定会发生什么.

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
            // [email protected]
            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" : "[email protected]","subtype" : "retina4","scale" : "2x"
}

Retina 4 2x出现在资产目录中,用于该图像集:)

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

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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...