2013-09-23 45 views
1

我设置一个随机的背景图片为我的应用程序通过加载图像并将其添加到我的视图缩放:设置视网膜背景图像出现在

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

我的图片640x1136在326 PPI,但图像出于某种原因放大。任何想法为什么会被赞赏。

模拟器

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

实际图像:

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

感谢。

+0

与PNG文件,而不是尝试。这可能是转换问题 –

+0

@Nick background.frame = self.view.frame;然后addSubView: – Spynet

+0

@ b3ginneriOS转换为PNG不幸的是似乎不工作。 –

回答

1

这是因为你的Alloc与你的形象初始化,不与固定大小。

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

做这样的

+0

什么是screenWidth和screenHeight? – Spynet

+0

我编辑了我的答案 – incmiko

+0

根据假设你对降采样是正确的假设改变了答案,尽管图像在iphone屏幕上看起来是一样的(可能太小而看不到)。它在你编辑之后起作用。谢谢。 –

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

挑选图像这不是正确的解决方案。图像将被缩减采样,它看起来就像在非视网膜手机上看起来一样。 – micantox

+0

我在iPhone5上测试这个,图像显示高分辨率,有什么想法? –

+0

我不知道降采样(或Spynet代码不好),但我知道,我的答案是正确的。 – incmiko