现在 Xcode 5 是否有一种聪明的方法可以根据它是否是 iOS7 在您的应用程序中加载不同的图像?
我能想到的最佳解决方案是在 iOS7 所需的图像末尾附加“_7”,然后在应用程序中使用图像时我可以:
NSString *OSSuffix = OSVersion == 7 ? @"_7" : @""; //would be define globally, also pseudo syntax
[UIImage imageNamed:[NSString stringWithFormat:@"imageName%@", OSSuffix]]; //can make a macro for this probably
但是否有更好的“内置”方式使用新 Assets 目录或其他方式来执行此操作?
最佳答案
I was wondering about a similar use case ,根据设备类型自动加载 568 像素高的图像。由于未提供该功能,我想出了一个 UIImage
的补丁,there’s a sample project here on GitHub :
+ (void) load
{
if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone) {
// Running on iPad, nothing to change.
return;
}
CGRect screenBounds = [[UIScreen mainScreen] bounds];
BOOL tallDevice = (screenBounds.size.height > 480);
if (!tallDevice) {
// Running on a 320✕480 device, nothing to change.
return;
}
method_exchangeImplementations(
class_getClassMethod(self, @selector(imageNamed:)),
class_getClassMethod(self, @selector(imageNamedH568:))
);
}
// Note that calling +imageNamedH568: here is not a recursive call,
// since we have exchanged the method implementations for +imageNamed:
// and +imageNamedH568: above.
+ (UIImage*) imageNamedH568: (NSString*) imageName
{
NSString *tallImageName = [imageName stringByAppendingString:@"-568h@2x"];
NSString *tallImagePath = [[NSBundle mainBundle] pathForResource:tallImageName ofType:@"png"];
if (tallImagePath != nil) {
// Tall image found, let’s use it. We just have to pass the
// image name without the @2x suffix to get the correct scale.
imageName = [imageName stringByAppendingString:@"-568h"];
}
return [UIImage imageNamedH568:imageName];
}
您可以使用相同的技巧根据一些自定义名称标签自动加载 iOS 7 资源。同样的警告也适用:UIImage
技巧使用方法调配,在生产中可能太神奇了。你来电。
关于ios - Xcode 5 使用不同的图像取决于是否使用 iOS 7 或更低版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19676323/