2015-11-03 196 views
1

我是iOS編程新手。我想問這些關於不同屏幕尺寸的圖像大小的問題不同iOS設備的圖像大小

  1. 對於所有iOS設備,x,2x和3x圖像大小是否足夠?我的意思是,如果我有一個名爲「background.png」的圖像,background.png,background2x.png和background3x.png是否足以適用於所有iOS設備/屏幕尺寸?
  2. 如果不是,不同的iPad型號是否需要其他圖像尺寸(x,2x和3x除外)? .......一個解釋不同的屏幕尺寸/設備的圖像大小的鏈接將不勝感激。由於

回答

0

如果您對@ 1倍,@ 2X形成的圖像,並@ 3倍,名稱他們是這樣的:

然後,你可以調用像吹製法:

UIImage *myImage = [self deviceSizedImageWithName:@"myImage.png"];

#define kScreenHeight [UIScreen mainScreen].bounds.size.height 
#define kScreenWidth [UIScreen mainScreen].bounds.size.width 
#define kScreenSize [UIScreen mainScreen].bounds.size 
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) 
#define IS_IPHONE_4 (IS_IPHONE && kScreenHeight == 480.0f) 
#define IS_IPHONE_5 (IS_IPHONE && kScreenHeight == 568.0f) 
#define IS_IPHONE_6 (IS_IPHONE && kScreenHeight == 667.0f) 
#define IS_IPHONE_6P (IS_IPHONE && kScreenHeight == 736.0f) 

- (UIImage *)deviceSizedImageWithName:(NSString *)imageNamed 
{ 
    NSString *imgExtension = [imageNamed pathExtension]; 
    NSString *imgName = [imageNamed stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@".%@", imgExtension] withString:@""]; 

    BOOL removedExt = [imgExtension length]; 

    UIImage *image = [UIImage imageNamed:imageNamed]; 
    if (IS_IPHONE_5) { 
     if (removedExt) image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-568h.%@", imgName, imgExtension]]; 
     else image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-568h", imageNamed]]; 

     if (!image) return [UIImage imageNamed:imageNamed]; 

    } else if (IS_IPHONE_6) { 
     if (removedExt) image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-667h.%@", imgName, imgExtension]]; 
     else image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-667h", imageNamed]]; 

     if (!image) return [UIImage imageNamed:imageNamed]; 

    } else if (IS_IPHONE_6P) { 
     if (removedExt) image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-736.%@", imgName, imgExtension]]; 
     else image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-736", imageNamed]]; 

     if (!image) return [UIImage imageNamed:imageNamed]; 

    } 
    return image; 
} 
相關問題