2013-10-30 32 views
1

現在有沒有一種巧妙的方式使用Xcode 5在您的應用中加載不同的圖像,取決於它是否是iOS7?Xcode 5根據使用iOS 7或更低版​​本使用不同的圖像

的最佳解決方案,我可以想出由具有「_7」附加在需要iOS7圖像的端部,然後在該應用中使用的圖像時,我可以去:

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 

但有一個更好地利用新的資產目錄進行「內置」方式或其他方式?

+0

資產目錄與此無關。僅僅因爲新版本的操作系統而需要新圖像是很不尋常的,所以這個功能是你需要實現的東西。 – borrrden

+0

一個解決方案是通過imageProvider傳送所有圖像,然後至少不需要在任何地方複製該代碼。此外:如果(NSFoundationVersionNumber> NSFoundationVersionNumber_iOS_6_1) –

+0

@borrrden當你的應用程序中有自定義按鈕和東西不像iOS 7的其餘部分那樣平坦時,它看起來有點奇怪,反之亦然iOS 6。目前的應用程序會遇到這種問題 – Fonix

回答

2

I was wondering about a similar use case,根據設備類型自動加載568像素高的圖像。由於該功能並沒有提供,我想出了一個補丁UIImagethere’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:@"[email protected]"]; 
    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技巧使用方法混合,並可能在生產中有太多的魔法。你的來電。

+0

非常好的解決方案,完美演示使用swizzling。 – Cyrille

相關問題