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 just tried this, and UIImage does not support _imageScaledToSize.
I ended up adding a method to UIImage using a category - a suggestion I found on the Apple Dev forums.
In a project-wide .h -
Implementation:
I think you can do something like
UIImageView+Scale.h:
UIImageView+Scale.m:
If the solutions proposed here aren't working for you, and your image asset is actually a PDF, note that XCode actually treats PDFs differently than image files. In particular, it doesn't seem able to scale to fill properly with a PDF: it ends up tiled instead. This drove me crazy until I figured out that the issue was the PDF format. Convert to JPG and you should be good to go.
Usually I use this method for my apps (Swift 2.x compatible):