Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scale a UIImageView proportionally?

I have a UIImageView and the objective is to scale it down proportionally by giving it either a height or width.

UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3092/2915896504_a88b69c9de.jpg"]]]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image];   //Add image view [self.view addSubview:imageView];     //set contentMode to scale aspect to fit imageView.contentMode = UIViewContentModeScaleAspectFit;  //change width of frame CGRect frame = imageView.frame; frame.size.width = 100; imageView.frame = frame; 

The image did get resized but the position is not at the top left. What is the best approach to scaling image/imageView and how do I correct the position?

like image 786
Ronnie Liew Avatar asked Oct 09 '08 01:10

Ronnie Liew


People also ask

What is the difference between a UIImage and a UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .

Does UIImageView have intrinsic content size?

Here is the essential problem: the only way in which UIImageView interacts with Auto Layout is via its intrinsicContentSize property. That property provides the intrinsic size of the image itself, and nothing more.

How do I change the UIImage size in Swift?

Show activity on this post. extension UIImage { func imageResize (sizeChange:CGSize)-> UIImage{ let hasAlpha = true let scale: CGFloat = 0.0 // Use scale factor of main screen UIGraphicsBeginImageContextWithOptions(sizeChange, ! hasAlpha, scale) self. draw(in: CGRect(origin: CGPoint.


1 Answers

Fixed easily, once I found the documentation!

 imageView.contentMode = .scaleAspectFit 
like image 120
Ken Abrams Avatar answered Sep 28 '22 08:09

Ken Abrams