MegaManX
MegaManX

Reputation: 8918

iPhone image stretched across whole imageView

- (void)viewDidLoad
{
  UIImage *image = [UIImage imageNamed:@"moon"];
  CGSize size = CGSizeMake(256, 256);
  UIGraphicsBeginImageContextWithOptions(size, YES, 1);
  CGRect imageRect = CGRectMake(50.0, 50.0, 128, 128);
  [image drawInRect: imageRect];
  image = UIGraphicsGetImageFromCurrentImageContext();
  UIImageView *iv = [[UIImageView alloc] initWithImage: image];
  [self setView: iv];
  [iv release];
  [super viewDidLoad];
}

This is the code that I call in my main controller. It should load image, draw it into rectangle, and then show it. And this code does that, with small problem: aspect ratio of image is not preserved - image height appears to be stretched, so whole image seems stretched. What is causing this?

Upvotes: 0

Views: 280

Answers (2)

holographix
holographix

Reputation: 2557

I think that your problem is the contentMode of your ImageView give it a try to

 [iv setContentMode:UIViewContentModeTop];

if you want it to be of its own size.

otherwise it you like it to be scaled or whatever, you just got a ton of options

UIViewContentModeScaleToFill
UIViewContentModeScaleAspectFit,
UIViewContentModeScaleAspectFill,
UIViewContentModeRedraw,
UIViewContentModeCenter,
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight, 

enjoy ;)

Upvotes: 1

Niko
Niko

Reputation: 2543

It's due to the content mode. Change it like this :

[iv setContentMode:UIViewContentModeScaleAspectFit];

Upvotes: 1

Related Questions