用继承的步骤去判断iPhone4与iPhone5的背景图

用继承的方法去判断iPhone4与iPhone5的背景图

在项目中我们有时候会遇到需要在背景贴上一个背景图的情况,但是这时候,iPhone4s和iPhone5的2倍图都是xxx@2x.png,这时候就需要我们特殊判断,一开始本来想着写一个类别来区分他们,但是发现会出现递归的情况,在参考了http://*.com/questions/9424004/suppress-warning-category-is-implementing-a-method-which-will-also-be-implement 这个文章之后,我写了一个继承.下面是代码


#define iPhone5s ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)

+ (UIImage *)imageNamed:(NSString *)name{
    
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        if (iPhone5s)
            return [UIImage imageNamed:[NSString stringWithFormat:@"%@-568h",name]]?[UIImage imageNamed:[NSString stringWithFormat:@"%@-568h",name]]:[UIImage imageNamed:name];
//            return [UIImage imageNamed:[NSString stringWithFormat:@"%@-568h@2x.png",name]];//不可以这样写
    
    return [UIImage imageNamed:name];
}




这样,就可以完美解决问题了