设置视网膜背景图像会放大

时间:2013-09-23 08:40:13

标签: iphone ios objective-c ios7

我正在为我的应用设置随机背景图片,方法是加载图片并将其添加到我的视图中:

UIImageView *background = [[UIImageView alloc] 
                               initWithImage:[UIImage imageNamed:
                               [NSString stringWithFormat:@"bg%u.jpg", 1+arc4random_uniform(15)]]];
    [self.view addSubview:background];
    [self.view sendSubviewToBack:background];

我的图像在326 ppi时为640x1136,但由于某种原因图像显示为放大。关于为什么会受到赞赏的任何想法。

模拟器

http://i.stack.imgur.com/dU38H.png

实际图片:

http://i.stack.imgur.com/TIm9F.png

感谢。

2 个答案:

答案 0 :(得分:1)

这是因为您使用图像分配init,而不是使用修复大小。

int screenHeight = [UIScreen mainScreen].bounds.size.height;
int screenWidth = [UIScreen mainScreen].bounds.size.width;


UIImageView *background = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];

background.image = [UIImage imageNamed:[NSString stringWithFormat:@"bg%u.jpg",1+arc4random_uniform(15)]]

[self.view addSubview:background];
[self.view sendSubviewToBack:background];

这样做

答案 1 :(得分:0)

你喜欢这样,

UIImageView *background = [[UIImageView alloc] 
                               initWithImage:[UIImage imageNamed:
                               [NSString stringWithFormat:@"bg%u.jpg", 1+arc4random_uniform(15)]]];

    background.frame = self.view.frame; // this is the only line you have missed 
    [self.view addSubview:background];
    [self.view sendSubviewToBack:background];