Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Image out of Bounds in iPhone Simulator

Hello,

My image in the crystalball app learning (iPhone Development) is going out of bounds and i can not spot the error.

Could you please help with the same. Thanking in advance

Regards Shekhar Agnihotri

Following is the Code :-

// Treehouse - learning

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize predictionLabel;
@synthesize predictionArray;

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *image = [UIImage imageNamed: @"crystal_ball copy@2x.png"];
    UIImageView *imageView =[[UIImageView alloc] initWithImage:image];
    [self. view insertSubview:imageView atIndex:0];


   self.predictionArray = [[NSArray alloc] initWithObjects:@"It is decideley so",
                                @"good things coming", 
                                @"Good time is here",
                                @"Great profit",
                                @"stay possitive",@"Stay Foccused",@"all is well",nil];

    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [self setPredictionLabel:nil];
    [self setPredictionLabel:nil];
    [self setPredictionLabel:nil];
    [self setPredictionLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)buttonPressed:(UIButton *)sender {

    NSUInteger index = arc4random_uniform(self.predictionArray. count);

    self.predictionLabel.text = [self.predictionArray objectAtIndex: index];
}
@end

5 Answers

Hey Shekhar,

I'm not exactly sure why your background image might not be centered properly. Do you have a UIImageView set up in your Storyboard/nib, or are you trying to do everything programmatically?

Also, as a side note, I'm not sure why you keep calling [self setPredictionLabel:nil]; in the - (void) viewDidUnload method.

Best,

J.T.

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

@Shekhar

I think the problem lies with this line:

    UIImage *image = [UIImage imageNamed: @"crystal_ball copy@2x.png"];

You should reference the regular image and not the @2x version, which is meant for the retina display. That line of code should look like:

    UIImage *image = [UIImage imageNamed: @"crystal_ball.png"];

Assuming the name of the file is "crystal_ball.png". To support the retina display the filename should have a suffix of @2x, which means the retina ready image should be named: "crystal_ball@2x.png". Although you don't reference that file in your code, you simply add it to your project.

@amit I have tried both the image types i.e. with @2x prefix and without@2x prefix - but no change visible. I am trying a different image this time around though. The line of code is exactly similar to the one i followed in your class. It will be great if you can help out. Thanks in advance.

And The Code for your reference once again

import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController @synthesize predictionLabel; @synthesize predictionArray;

  • (void)viewDidLoad { [super viewDidLoad];

    UIImage *image = [UIImage imageNamed: @"dc-crystal-ball.jpg"]; UIImageView *imageView =[[UIImageView alloc] initWithImage:image]; [self. view insertSubview:imageView atIndex:0];

self.predictionArray = [[NSArray alloc] initWithObjects:@"It is decideley so", @"good things coming", @"Good time is here", @"Great profit", @"stay possitive",@"Stay Foccused",@"all is well",nil];

// Do any additional setup after loading the view, typically from a nib.

}

  • (void)viewDidUnload { [self setPredictionLabel:nil]; [self setPredictionLabel:nil]; [self setPredictionLabel:nil]; [self setPredictionLabel:nil]; [super viewDidUnload]; // Release any retained subviews of the main view. }

  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); }

  • (IBAction)buttonPressed:(UIButton *)sender {

    NSUInteger index = arc4random_uniform(self.predictionArray. count);

    self.predictionLabel.text = [self.predictionArray objectAtIndex: index]; } @end

Regards Shekhar Agnihotri

@JT

Yes sir I am trying to do things programatically. Also since Iam new to development could you be more elaborate as to why I should avoid the following line of code you mentioned in your post :

"Also, as a side note, I'm not sure why you keep calling [self setPredictionLabel:nil]; in the - (void) viewDidUnload method."

Regards SA

@ Amit Bijlani and @ JT - I found the solution and it is the size of the image - which if i kept at width-4in and height-5.9 inches worked just fine for me.

Regards Shekhar Agnihotri