Iphone 4 と Iphone 5 で異なる UIImage を表示するにはどうすればよいですか?
iPhone 3,4 と iPhone 5 で異なるサイズの画像を表示したいのですが、どうすればいいですか?
Iphone 4 と Iphone 5 で異なる UIImage を表示するにはどうすればよいですか?
iPhone 3,4 と iPhone 5 で異なるサイズの画像を表示したいのですが、どうすればいいですか?
これは横向きの方法ですが、非常に迅速です。
int screenHeight = self.view.bounds.size.height;
if (screenHeight<500) {
//Show the UIImage for iPhone 4.
}
else {
//Show the UIImage for iPhone 5.
}
UIKit supports the specification of a @2x
device modifier in order to select a retina-enabled version of an image through the imageNamed
method.
This will allow to manage a non-retina and a retina version of the same image. Unfortunately, there is no provision for automatically handling an iPhone5-version of an image (except for Default.png). You will have to do that on your own.
Or, you can use this category to be able to use a -568h@2x
device extension for your iPhone5-specific image:
+ (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 ];
}