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

View Mode

How do I change the View mode of an image? I think by default the image is set for top left but I want to change it to Aspect Fill or Aspect Fit.

1 Answer

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

From your Storyboard select your Image View and then from the Attributes Inspect (in the Utility area on your right) you can change it using the "Mode" drop-down.

Hello Amit!

I tried that, but it does not work. I think its because I am using the UIIamgeView as a IBOutlet

@property (strong, nonatomic) IBOutlet UIImageView *imageView;

Then I'm using it later to change images: UIImage *image = [UIImage imageNamed:@"Photo1.png"]; self.imageView = [[UIImageView alloc] initWithImage:image]; [self.view insertSubview:self.imageView atIndex:0];

self.imageView.animationImages = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"Photo2"], [UIImage imageNamed:@"Photo3"], [UIImage imageNamed:@"Photo4"], nil];

Any suggestions?

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

Then you can simply modify it in your code. After allocating and initializing your imageView you can change the contentMode property:

self.imageView = [[UIImageView alloc] initWithImage:image];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view insertSubview:self.imageView atIndex:0];

You can check out the entire list of content modes

Nope: Do you think its because my images are too big? (640×1136) Just in case the code I changed is below:

UIImage *image = [UIImage imageNamed:@"Photo1.png"];
self.imageView = [[UIImageView alloc] initWithImage:image];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view insertSubview:self.imageView atIndex:0];
Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

Do you still have an image view in your storyboard? Not sure why you have an IBOutlet defined if you are adding an image view within your code. If you do have an image view on your storyboard then it's possible it is hiding the one you are adding using the code. Remove one or the other.

I don't know what you are expecting the image to do because I can't tell what is going on visually. You might want to try some of the other content modes using the list I provided above.

No I removed the IBOutlet. I'll try some other modes as well.

.h @property (strong, nonatomic) IBOutlet UILabel *predictionLabel; @property (strong, nonatomic) NSArray *predictionArray; @property (strong, nonatomic) UIImageView *imageView;

.m UIImage *image = [UIImage imageNamed:@"yoda"]; self.imageView = [[UIImageView alloc] initWithImage:image]; self.imageView.contentMode = UIViewContentModeScaleAspectFit; [self.view insertSubview:self.imageView atIndex:0];

I want this Should be

But it looks like this looks like this