ios - Xcode 5 使用不同的图像取决于是否使用 iOS 7 或更低版本

标签 ios objective-c xcode

现在 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/

相关文章:

ios - 如何为 iMessage Sticker App Grid Sticker 提供不同尺寸

objective-c - 获取数据图像进行处理的最佳方式

objective-c - 具有长输出的 NSTask (OSX)

ios - UITableView 重新排列单元格

objective-c - 将 NSRegularExpression 重写为 RegexKit

ios - 如何更正聊天和消息弱关系的谓词 CoreData Fetched 属性?

ios - 我希望我可以使用 ios sdk 将相机的快门声音静音

iOS 蓝牙键盘未注册 VoiceOver control+option/(CTRL+ALT) 键同时按下

ios - UserInteractionEnabled 是否像 UIView 层次结构的 Alpha 一样工作?

ios - 如何更改页面 Controller 点图像?