25

I am very new to objective c and I'm just getting my bearings. I want to do something really simple but it proves to be quite a challenge:

I am trying to display an image into an UIImageView. The image I'm showing is large and I want it scaled down to fit the UIImageView. I tried setting the AspectFit View mode but the image gets displayed to the original size and is clipped by the UIImageView. My code is below:

- (void)changeImages
{
    UIImage* img11 = nil;

    img11 = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"jpeg"]];

    u11.contentMode = UIViewContentModeScaleAspectFit;
    u11.image = img11;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self changeImages];
}

Can anyone shed some light on this please?

Thanks!

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
cristi71000
  • 1,094
  • 2
  • 10
  • 16
  • 1
    Did you read http://stackoverflow.com/questions/185652/how-to-scale-a-uiimageview-proportionally ? Especially the second answer might be of interest. – FD_ Sep 23 '12 at 13:58
  • By all rights, this should work. Things to check: Are you already setting the image in the nib/storyboard? If so, this could mask code not getting called. Is changeImages getting called? You can place a call to `NSLog(@"changeImages called")` to see if it is, or use a breakpoint. If it isn't, you may have forgotten to change the class of the ViewController in your nib/storyboard to the class of the code you wrote above. Is `u11` specified as an IBOutlet and correctly linked up to the nib/storyboard? – Xono Sep 23 '12 at 14:03
  • If you are very new to objective c maybe the best is to use third library about image resizing such as https://github.com/mattgemmell/MGImageUtilities – martinezdelariva Sep 23 '12 at 14:23
  • What is the view hierarchy around `u11` ? Maybe it's not the UIImageView that is clipping the image, but a parent view? – Bryan Sep 23 '12 at 14:45
  • What's the frame size of `u11`? Is its `autresizesSubviews` property set to `YES`? – Carl Veazey Sep 23 '12 at 15:48

6 Answers6

32

Hi I would try this...

- (void)changeImages
{
    UIImage *img11 = [UIImage imageNamed@"dog.jpeg"];

    u11.contentMode = UIViewContentModeScaleAspectFit;
    u11.clipsToBounds = YES;
    [u11 setImage:img11];
}

- (void)viewWillAppear:animated
{
    [super viewWillAppear:animated];

    [self changeImages];
}

This will scale the image (up or down) so that it fits inside the imageView. Having clipsToBounds isn't necessary but will stop the image from displaying outside the frame of your imageView.

HTH.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • In Swift 5 - UIViewContentModeScaleAspectFit is now "UIView.ContentMode.scaleAspectFit", and YES is now "true". Note that you might want to use the scaleAspectFill instead of scaleAspectFit, depending on the scenario. – Andy Weinstein Apr 28 '21 at 18:24
17

Add to your UIViewController.m:

-(UIImage *)resizeImage:(UIImage *)image imageSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0,0,size.width,size.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    // here is the scaled image which has been changed to the size specified
    UIGraphicsEndImageContext();
    return newImage;
}

Using:

UIImage *image = [UIImage imageNamed:@"image.png"];
CGSize size = CGSizeMake(50, 63); // set the width and height 
UIImage *resizedImage = [self resizeImage:image imageSize:size];

I hope it helps.

zakishaheen
  • 5,551
  • 1
  • 22
  • 29
Erhan Demirci
  • 4,173
  • 4
  • 36
  • 44
  • So if I have a UITableView that is getting my data from online to populate the UITableViewCells picture and I want to resize my image this way to improve performance, do I call this method in cellForRowAtIndexPath? – Brandon A Dec 05 '14 at 10:36
4
CGSize size=CGSizeMake(79, 84);//set the width and height
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0,0,size.width,size.height)];
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
//here is the scaled image which has been changed to the size specified
UIGraphicsEndImageContext();

This works for sure and don't forget to import QuartzCore FrameWork..

Have a Happy Coding (^_^)....

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
1

You can also set View:Mode to Aspect Fit in the Attributes Inspector in Interface Builder

Tad
  • 4,668
  • 34
  • 35
0

I did the following and it helped change the mode to "aspect fill" from the default value "Scale to fill"

and add a line of code as follows (I did it in a cell configuration):

cell.photo.clipsToBounds = true
Gulz
  • 1,773
  • 19
  • 15
0

I know this is old but I wanted to add a response based on the Stanford 193p 2017 lecture 11 (around 18:45) and for anyone looking in swift as this is the first search result that showed up for me.

Basically, subclass UIView and make it look like:

class U11: UIView {
    var myImage: UIImage? { didSet { setNeedsDisplay() }}
    override func draw(_ rect: CGRect) {
        myImage?.draw(in: bounds)
    }
}

Then set the image with:

func changeImage() {
    if let img11 = UIImage(named: "dog.jpeg"){
            u11.myImage = img11
    }
}

This is super simple and the image takes up the whole space inside of the views bounds.

vallard
  • 1,028
  • 1
  • 9
  • 14