iOS6中的背景图片匹配

随着iphone5的推出,iOS家族又多了一个尺寸,320*568,这就意为着在iOS开发中我们要适配这个新的分辨率。

在老的iOS应用中,背景图片一般为320*480,在iphone5中会进行拉伸,特别是在背景图片和控件叠加使用的情况下,会造成控件位置和图片对应位置的偏移,那么我们只能通过提供320*568大小的图片来解决,但是ios6中不会识别-568h@2x后缀的图片。因此我们需要自己扩展UIImage方法,来实现系统自动识别a.png,a@2x.png ,a-568h@2x.png.

@implementation UIImage (iPhone5extension)
+ (UIImage*)imageNamedForDevice:(NSString*)name {

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
if (([UIScreen mainScreen].bounds.size.height * [UIScreen mainScreen].scale) >= 1136.0f)
{
//Check if is there a path extension or not
if (name.pathExtension.length) {
name = [name stringByReplacingOccurrencesOfString: [NSString stringWithFormat:@".%@", name.pathExtension]
withString: [NSString stringWithFormat:@"-568h@2x.%@", name.pathExtension ] ];

} else {
name = [name stringByAppendingString:@"-568h@2x"];
}

//load the image e.g from disk or cache
UIImage *image = [UIImage imageNamed: name ];
if (image) {
//strange Bug in iOS, the image name have a “@2x” but the scale isn’t 2.0f
return [UIImage imageWithCGImage: image.CGImage scale:2.0f orientation:image.imageOrientation];
}

}
}

return [UIImage imageNamed: name ];

}

@end

使用的时候需要手动的去设置背景图片,而不能在Interface builder中去设置

self.imageView.image = [UIImage imageNamedForDevice:@”login.png”];

This entry was posted in iOS, iphone5.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">