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?
I've seen a bit of conversation about scale types so I decided to put together an article regarding some of the most popular content mode scaling types.
The associated image is here:
I used following code.where imageCoverView is UIView holds UIImageView
one can resize an UIImage this way
This works fine for me Swift 2.x:
Set your
UIimageview
by scale.........The original code posted at the top worked well for me in iOS 4.2.
I found that creating a CGRect and specifying all the top, left, width, and height values was the easiest way to adjust the position in my case, which was using a UIImageView inside a table cell. (Still need to add code to release objects)