【发布时间】:2016-01-02 03:05:56
【问题描述】:
我看过各种关于图像尺寸的旧帖子,但我找不到任何最新信息,甚至不知道是否可以仅使用资产目录来提供适用于所有 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
// 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;
【问题讨论】:
-
我问的是同一个 ************ 问题,但似乎没有人在意。这就像,超级重要为什么没有人回答? (顺便说一句,你最终对 iPad 的图像做了什么……?)
-
我最终使用绘图代码来绘制我的背景,事实上我几乎已经替换了所有图像。就我而言,我需要大量调整事物的大小,因此重新生成图像是不切实际的。
-
所以您决定稍微降低图像质量并最小化二进制文件的大小..?我正在寻找的是两者的解决方案。我不想失去一点质量,同时也不想获得二进制大小......
-
不,没有质量损失,使用油漆代码,结果总是清晰的
-
Paint code 是一个包,可生成您在应用程序中使用的代码。
标签: ios iphone xcode xcode7 xcasset