我的一些应用使用自定义图片作为背景。检查屏幕尺寸以放置正确图像的正确方法是什么?
在viewDidLoad中是否应该是这样的:
if ([UIScreen mainScreen] == 2.0)
{
UIImage * backgroundImage = [UIImage imageNamed:@"bgimage-568h@2x.png"];
backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage]];
}
else
{
UIImage * backgroundImage = [UIImage imageNamed:@"bgimage.png"];
backgroundImageView = [[UIImageView alloc] iniWithImage:backgroundImage]];
}
非常感谢任何提示/建议!
谢谢!
答案 0 :(得分:16)
以下代码检查屏幕的大小/范围。如果屏幕是586点(请记住,屏幕是由于Retina测量的),我们知道这是新的4英寸Retina显示屏。
if ([[UIScreen mainScreen] bounds].size.height == 568)
{
UIImage * backgroundImage = [UIImage imageNamed:@"bgimage-568h@2x.png"];
backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
}
else
{
UIImage * backgroundImage = [UIImage imageNamed:@"bgimage.png"];
backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
}
答案 1 :(得分:1)
[self.bgImageView setImage:backgroundImage];
而不是
backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
如果我使用前者,则仅显示正确的图像。