我看过各种有关图像尺寸的旧帖子,但我找不到任何最新内容,甚至不知道是否可以仅使用资产目录来提供所有 iPad 和 iPhone 屏幕尺寸的图像。
这是我找到的最好的帖子,但在 Xcode 7 中它没有显示“Retina 4 2x”或 iPhone 6 / 6+
Xcode 6 - 用于通用图像支持的 xcassets https://stackoverflow.com/questions/26020979/xcode-6-xcassets-for-universal-image-support?lq=1
在 Xcode 7 中有一个通用选项,但是这三个图像并不支持所有设备尺寸。
我已经看到了一些选项,您可以在资产目录之外提供自己的图像,但我真的很想使用资产目录。
如何在不同的iPhone/iPad上使用xcassets/通用背景图片? https://stackoverflow.com/questions/29100590/how-to-use-xcassets-universal-background-images-for-different-iphones-ipads
编辑:
看起来我可能不得不选择无资产目录路线:(
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
// [email protected] /cdn-cgi/l/email-protection
launchImageName = [NSMutableString stringWithFormat:@"%@.png", launchImageName];
}
}
return [UIImage imageNamed:launchImageName];
}
@end
免责声明:摘自https://github.com/Daij-Djan/DDUtils https://github.com/Daij-Djan/DDUtils
C)
这看起来也不错,但它正在重新调整大小,而不是使用实际的清晰图像,并且没有回退。
https://gist.github.com/kevindelord/fe2e691d06ab745fbb00 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;