Change UIImageView height to image height? - ios

I have a UIImageView set up which will display an image determined by what the user selected in the previous table view.
This is how I am currently displaying my images:
if ([_TitleLabel.text isEqualToString:#"Dog"]) {
UIImage *image = [UIImage imageNamed:#"Dog.png"];
[imageView setImage:image];
}
Subsequent if statements follow displaying the different images for a different animal.
These images need to be of a different height, and I need to keep their proportionality. The width of all the images is 320 pts. The heights vary from anywhere between 1000pts to 2500pts. All images are located locally in the project. I want the height of the UIImageView to change to the height of image which corresponds to the animal selected by the user. Any ideas?

You can update the imageView's frame width and height using UIImage's width and height, like this:
if ([_TitleLabel.text isEqualToString:#"Dog"]) {
UIImage *image = [UIImage imageNamed:#"Dog.png"];
[imageView setImage:image];
// update image view frame width and height.
imageView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y, image.size.width, image.size.height);
}
Note:
Using UIViewContentModeScaleAspectFit will leave space between your image and the UIImageView boundary if the image is different aspect ratio to your UIImageView frame, which isn't likely what you want.
Nor will UIViewContentModeScaleAspectFill help, your image will become truncated within the frame of your imageView.

imageView.contentMode = UIViewContentModeCenter;
if (imageView.bounds.size.width > ((UIImage*)imagesArray[i]).size.width && imageView.bounds.size.height > ((UIImage*)imagesArray[i]).size.height) {
imageView.contentMode = UIViewContentModeScaleAspectFit;
}

Related

How to fit an image on a uiview programmatically

I added a uiview as a subview on a view controller programmatically (called contentView). I also added an image on that uiview programmatically. Every time I run the app on the iPad the image is stretched! How do I fix the image so that it fits the iPad screen but doesn't stretch the image? I know the drawInRect is what is stretching the image. So how do I fix that?
Here is my code:
UIGraphicsBeginImageContextWithOptions(self.contentView.bounds.size, self.contentView.opaque, 0.0);
[[UIImage imageNamed:#"menu image 2.JPG"] drawInRect:self.contentView.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.contentView addSubview:imageView];
UIImageView has a property called contentMode which determines how the image layout is handled in the view context. contentMode is of type UIViewContentMode.
The default value is UIViewContentModeScaleToFill which stretches the image without respecting the aspect ratio. I am assuming it is the changing aspect ratio that is causing the issue.
If you wish to scale the image, but keep the aspect ratio, you have two options:
UIViewContentModeScaleAspectFit: This will show the image scaled to fill the view, but not clip any contents (if the aspect ratio doesn't match view size, it will show either horizontal or vertical bands)
UIViewContentModeScaleAspectFill: This will scale the image to fill the view entirely, without any bands - this will result in content being clipped if the image ratio doesn't match the view ratio.
To set the contentMode on the image view in Objective-C, use the following syntax:
UIImage *image = [UIImage imageNamed:#"menu image 2.JPG"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFill;
...
You should not need to use the custom context drawing for this to work anymore (thanks to Losiowaty for asking me about this).
Change code as below
UIImage *image = [UIImage imageNamed:#"menu image 2.JPG"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.contentView addSubview:imageView];
I guess your self.contentView.bounds is not having the same aspect ratio as your menu image 2.JPG . For an experiment, please try looking at the menu image 2.JPG's resolution. For example if it is 300x150, it's aspect ratio is (300/150 = 2:1). Set your self.contentView.bounds to this aspect ratio and draw the image and check the results. It will not stretch.
Please add this Single Line of code in your project
yourImage .contentMode = UIViewContentModeScaleAspectFit;

Set image in UIImageView with original Dimension in ios

My UIImageView with constant Width*Height is 100*100 on UIView, But image i want to show in this UIImageView is 25*25 (original dimension ).
I don't want to stretch image. i try UIImageView ContentMode property but did't work.
This i don't want.tried sizeToFit and aspectCenter mode.
I need following result,For small images
How to do that(With out stretching original image dimension and constant UIImageView dimension).What will be case if image is bigger than UIImageView ? Thanks in advance.
Try this
(Objective-C):
imageView.contentMode = UIViewContentModeCenter;
(Swift):
imageView.contentMode = UIViewContentMode.Center;
Call - sizeToFit
That should do exactly what you need.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/index.html#//apple_ref/occ/instm/UIView/sizeToFit
you should try this
imageView.contentMode = UIViewContentModeScaleAspectFit;
no matter image is small or big it will scale the image.
if you don't want to stretch image than you have to resize UIImageView to image size
You can obtain image size from UIImage.
for eg
UIImage *image = [UIImage imageNamed:#"imageName"];
float imgHeight = image.size.height;
float imgWidth = image.size.width;

Setting UIImageView contentMode shows no effect

I have a UIImageView which is sized to 366 x 375 (confirmed by NSLog statement) and a UIImage which is is sized to 400 x 600 (again confirmed with a log statement). I have tried setting the contentMode to preserve the image's aspect ratio, but when I run my app, the image is always distorted.
I tried setting the UIView's content mode to both UIViewContentModeScaleAspectFit and UIViewContentModeScaleAspectFill in turn. In both cases, the photo is still distorted in the same way. I also tried, just for kicks, UIContentModeLeft, which also resulted in the same distort presentation. My understanding is that these 3 modes should have presented very different images.
I've done a ctrl-f through the view controller code, but there is only once occurrence of contentMode and it is where I am setting the property. Are there other properties I should look at that could be interfering?
Here is the code that sets up the image view and accompanying image. This is the only code in the whole project that refers to the image view. Also I am not using any sort of auto layout features, though I don't see why that should affect aspect ratio and content mode anyhow.
NSData *imageData = [NSData dataWithContentsOfURL:filePath];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.height*.11, self.width, self.height*.55)];
self.imageView.image = [UIImage imageWithData:imageData];
NSLog(#"Image height and width are %f and %f", self.imageView.image.size.height, self.imageView.image.size.width);
NSLog(#"imageview height and width are %f and %f", self.imageView.frame.size.height, self.imageView.frame.size.width);
[self.view addSubview: self.imageView];
Why doesn't the contentMode affect how my image is displayed, and how can I fix this? Ultimately I want to use the scale aspect fill option so there is no empty space within the image view but so that the aspect is also preserved.
Try this
self.imageView.contentMode = UIViewContentModeScaleAspectFill
EDIT
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.height*.11, self.width, self.height*.55)];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.image = [UIImage imageWithData:imageData];
NSLog(#"Image height and width are %f and %f", self.imageView.image.size.height, self.imageView.image.size.width);
NSLog(#"imageview height and width are %f and %f", self.imageView.frame.size.height, self.imageView.frame.size.width);
[self.view addSubview: self.imageView];
Set the content mode after u allocate the imageview

UIImageView with fixed width: how to set its height to keep its aspect?

I have a view where I need to place an UIImageView, and I was told to place it inside a rectangle that takes the screen width and its height is smaller than width (fixed size). However, the images I've been given are more or less square jpeg images, so the idea is to fill the width of the rectangle that should contain the image and set the height of the image in a way that its aspect ratio is kept. Then, I should be able to let the user scroll the image vertically to see the complete image.
How could I do this?
Thanks in advance
EDIT: I need to do this for several images that have different sizes, but that should fit the same rectangular area size within the view
You can set imageview content mode.
imageView.contentMode = UIViewContentModeScaleAspectFit;
This will make sure that the image is displayed by keeping original aspect ratio.
Edit:
I think this is what you wanted:
UIImage *originalImage = [UIImage imageNamed:[picArr objectAtIndex:a]];
double width = originalImage.size.width;
double height = originalImage.size.height;
double apect = width/height;
double nHeight = 320.f/ apect;
self.img.frame = CGRectMake(0, 0, 320, nHeight);
self.img.center = self.view.center;
self.img.image = originalImage;
Hope this helps.. :)

How do I make a UIImageView not be wider than the view it's contained in?

I set the image of a UIImageView to an image that is 1024x1024, and as a result a lot of the image is not visible, especially width wise, and cut off the edges of the screen.
I tried using clipsToBounds:
UIImageView *imageFromLink = [[UIImageView alloc] initWithImage:responseObject];
imageFromLink.clipsToBounds = YES;
[darkOverlayView addSubview:imageFromLink];
But it doesn't seem to do anything, and the image is still too big for the view.
You're going to want to change the contentMode property for the desired effect. UIViewContentModeScaleAspectFill is most likely what you'll want.
You have the set the image view's frame to the size you want. By default it will be the size of the image.
UIImageView *imageFromLink = [[UIImageView alloc] initWithImage:responseObject];
imageFromLink.contentMode = UIViewContentModeScaleAspectFit;
imageFromLink.frame = CGRectMake(0, 0, desiredWidth, desiredHeight);
[darkOverlayView addSubview:imageFromLink];
Also set the contentMode so the image is scaled properly in the smaller image view.
You should set the imageview's frame equal to the container's bounds.
UIImageView *imageFromLink = [[UIImageView alloc]initWithImage:responseObject];
imageFromLink.contentMode = UIViewContentModeScaleAspectFit;
imageFromLink.frame = darkOverlayView.bounds;
[darkOverlayView addSubview:imageFromLink];

Resources