简体   繁体   中英

Resizing image to fit UIImageView

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!

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.

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.

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 (^_^)....

您还可以在Interface Builder中的“属性”检查器中将View:Mode设置为Aspect Fit。

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM