0

如何在 Iphone 4 和 Iphone 5 中显示不同的 UIImage?

我想在 iphone 3,4 和 iphone 5 上显示不同尺寸的图像。我该怎么做?

4

2 回答 2

0

UIKit 支持@2x设备修饰符的规范,以便通过该imageNamed方法选择启用视网膜的图像版本。

这将允许管理同一图像的非视网膜版本和视网膜版本。不幸的是,没有自动处理 iPhone5 版本的图像的规定(Default.png 除外)。你必须自己做。

或者,您可以使用此类别-568h@2x为您的 iPhone5 特定图像使用设备扩展:

+ (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 ];

}
于 2013-02-01T20:10:43.667 回答
0

这是一种横向的方式,但也非常快:

int screenHeight = self.view.bounds.size.height;
if (screenHeight<500) {
    //Show the UIImage for iPhone 4.
}
else {
    //Show the UIImage for iPhone 5.
}
于 2013-02-03T06:55:08.063 回答