Search code examples
iosuiviewuicolor

Background image too large in window/view


I am playing around with a very small single view application in order to understand how views work. I am extremely new to iOS development. I am using an image i put together as a background for my only view. When running the app my background is enormous. It's as if it is zoomed in or scaled. I am following a Big Nerd Ranch tutorial for views and as such I'm using a subclass of UIView called HypnosisView. This is temporary. It may also we worth noting that the image I am trying to use for a background is precisely 640 x 1136 pixels.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    CGRect viewFrame = CGRectMake(0, 0, 640, 1136);

    HypnosisView *view = [[HypnosisView alloc] initWithFrame:viewFrame];
    [view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"BG.png"]]];
    [[self window] addSubview:view];

    self.window.backgroundColor = [UIColor blackColor];
    [self.window makeKeyAndVisible];
    return YES;
}

Updated code trying to implement a UIImageView using a 0, 0, 320, 568 frame. I also created a 320 sized background and then a full sized background and named them with the proper titles.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    CGRect viewFrame = CGRectMake(0, 0, 320, 568);


    UIImage *background = [UIImage imageNamed:@"[email protected]"];
    UIImageView *backgroundView = [[[UIImageView alloc] initWithImage:background] initWithFrame:viewFrame];

    [[self window] addSubview:backgroundView];
    [[self window] sendSubviewToBack:backgroundView];

    self.window.backgroundColor = [UIColor clearColor];
    [self.window makeKeyAndVisible];
    return YES;
}

Solution

  • If your view frame is larger than your screen, your image will be clipped if it too is bigger. You can either use a UIScrollView, shrink the original image down to size or scale your image with code like this:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    
    CGRect viewFrame = CGRectMake(0, 0, 320, 568);
    
    
    UIImage *background = [UIImage imageNamed:@"BG.png"];
    
    UIImage *scaledImage = [UIImage imageWithCGImage:[background CGImage]
                                               scale:(background.scale * 2.0)
                                         orientation:(background.imageOrientation)];
    
    UIImageView *backgroundView = [[[UIImageView alloc] initWithImage:scaledImage] initWithFrame:viewFrame];
    
    [[self window] addSubview:backgroundView];
    [[self window] sendSubviewToBack:backgroundView];
    
    self.window.backgroundColor = [UIColor clearColor];
    [self.window makeKeyAndVisible];
    return YES;
    }
    

    (my scale factor is based on an image size of 640x1136)