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

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

Actual image:

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

Thanks.

Vinodh
  • 5,262
  • 4
  • 38
  • 68

2 Answers2

1

It s because you alloc init with your image, not with a fix size.

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

Do it like this

Magyar Miklós
  • 4,182
  • 2
  • 24
  • 42
  • what is screenWidth and screenHeight ? – Arun Sep 23 '13 at 09:03
  • changed answer to this based on the assumption that you're correct about downsampling, though the images look the same on the iphone screen (maybe it's too small to see). It works after your edits. Thanks. – Nick Triantafillou Sep 23 '13 at 09:22
  • This answer is not from me, and this solution is not any different than that of spynet! In fact, i think i might have been wrong (i was testing a couple of things) – micantox Sep 23 '13 at 09:40
  • I don't see a solution from you at all, just comments that none of them are correct. – Nick Triantafillou Sep 23 '13 at 09:45
0

Hi try like this,

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];
Arun
  • 3,406
  • 4
  • 30
  • 55