如果针对iPhone5和非iPhone5设备做了两套界面,该代码会自动判断设备是否是iPhone5,然后调用相应的图片资源。
比如非iPhone设备图片名称是 cc.png ,那么iPhone5的图片名称就是cc-ip5.png 。
写的时间不长,没有真机iPhone5做测试,模拟器没问题。如果真机有问题,请大家帮忙改改啊。
github地址:
https://github.com/fisherlee/UIImageWithiPhone5
提示下 ,在myproject-Prefix.pch中 import 'UIImage+iPhone5' ,就可以在项目引用了。
代码
UIImage+iPhone5.h
#import
@interface UIImage(iPhone5)
+ (UIImage *)imageNamedWithiPhone5:(NSString *)name imageTyped:(NSString *)type;
@end
UIImage+iPhone5.m
#import "UIImage+iPhone5.h"
@implementation UIImage(iPhone5)
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
+ (UIImage *)imageNamedWithiPhone5:(NSString *)name imageTyped:(NSString *)type
{
NSString *imgName = nil;
if ([type length]==0) {
type = @"png";
}else
{
type = type;
}
if (iPhone5) {
imgName = [NSString stringWithFormat:@"%@-ip5",name];
}else{
imgName = name;
}
NSString *path = [[NSBundle mainBundle] pathForResource:imgName ofType:type];
UIImage *image = [UIImage imageWithContentsOfFile:path];
return image;
}
@end