Need help setting background image.

Discussion in 'iOS Programming' started by Abrexas, Dec 30, 2011.

  1. Abrexas macrumors member

    Joined:
    Jul 29, 2011
    #1
    I'm sure this question has been asked and answered several times before, but I am having the hardest time finding a solution.

    What I'm trying to do is have the background of my app change when the screens orientation changes. I have two custom images that I have made that I would like to use as the backgrounds, but I just cannot figure out how to get this to work.

    Both images have been put into my resources folder, and I know how to set a UIImageView through IB, but to do it programmatically is something I haven't yet learned.

    Any suggestions?

    Thanks,
    - Steven French
     
  2. Abrexas thread starter macrumors member

    Joined:
    Jul 29, 2011
    #2
    Finally figured it out with:

    Code:
    -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
        
        UIDeviceOrientation toInterfaceOrientation = [[UIDevice currentDevice] orientation];
        
        if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        {
            UIImage *img = [UIImage imageNamed:@"DREDNT_port.jpg"];
            [backgroundImage setImage:img];
            
        }
        else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
        {
            UIImage *img = [UIImage imageNamed:@"Pledge_land.jpg"];
            [backgroundImage setImage:img];
        }
        
    }
    This creates a slight lag when changing, but that is not my worry. If anyone knows of a better solution than I would love to lear it. ^_^
     
  3. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    If you move your code into:
    Code:
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    you might be able to remove the lag.
     
  4. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #4
    You also might not want to use JPG's.. In any case, PNG's are the way to go for iOS development, for the simple reason of packaging, loading because of the properties of PNG's, they might be a big bigger, but in the end, it's the best!
     

Share This Page