Brad-ley
Brad-ley

Reputation: 43

UIImageView is showing the image zoomed in

Not sure what I'm doing wrong but then viewing the image I just took using my app the image is zoomed in to a particular place on the original image and I have no idea why.

Heres my code for loading and displaying the image.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:path]];
imageView.contentMode = UIViewContentModeScaleAspectFill;

Upvotes: 1

Views: 1297

Answers (2)

Magyar Miklós
Magyar Miklós

Reputation: 4272

It's because you alloc init with the image, not with a fix size. Your image is bigger than you think, and it's look like "zoomed".

Do it like this:

UIImageView* yourImageView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
yourImageView.image = [UIImage imageWithContentsOfFile:path];
yourImageView.contentMode = UIViewContentModeScaleAspectFill;
yourImageView.clipsToBounds = YES;

Upvotes: 1

lxt
lxt

Reputation: 31304

Your image is being zoomed because you've told the view to do it:

imageView.contentMode = UIViewContentModeScaleAspectFill;

In plain English, this is telling the image view to "make the image fit the view by zooming it without adjusting the aspect ratio".

If you don't want to zoom the image you have a few options. You can make sure your UIImageView is the same aspect ratio as the image you want to display in it, or you can change the content mode to something else (such as UIViewContentModeScaleToFill, which will stretch your image to fit, or UIViewContentModeCenter which will center it).

Upvotes: 1

Related Questions