1

I'm setting a random background image for my app by loading an image and adding it to my view:

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

My images are 640x1136 at 326 ppi, yet the image is appearing zoomed in for some reason. Any ideas as to why would be appreciated.

Simulator

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

Actual image:

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

Thanks.

4

2 回答 2

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

像这样做

于 2013-09-23T08:59:32.087 回答
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];
于 2013-09-23T08:58:44.330 回答