1

I have the UIImage like so:

let imageView = UIImageView(image: image!)

imageView.contentMode = .scaleToFill

self.view.addSubview(imageView)

What I am trying to do it make the image full width of the screen, I have tried scaleToFill, scaleAspectFit and scaleAspectFill neither of them do anything...what am I doing wrong?

Here is my full code:

getLandGradingImage(image: imageData!) { result in
    let image = UIImage(data: result)

    if image != nil {
        let imageView = UIImageView(image: image!)

        imageView.contentMode = .scaleAspectFit

        //Add Image to view
        self.view.addSubview(imageView)
    } else {
        self.customAlert(title: "Error", message: "Unable to get images")
    }

}
3
  • There is too little information here to determine anything. Where are you giving the image view a size and position? What are you adding the image view to? Is this in a view controller? Which method are you adding it in? Are you using AutoLayout? How big is the image? Commented May 23, 2018 at 14:16
  • The image size is 1024, no I am not using AutoLayout, this is in a view controller Commented May 23, 2018 at 14:18
  • I suggest adding the UIImageView to the view BEFORE downloading the image. Use autolayout constraints to position it and size it. Then just set the image in the completion here. Commented May 23, 2018 at 14:22

3 Answers 3

3
let imageView = UIImageView(image: image!)
imageView.contentMode = .scaleAspectFill
imageView.frame = self.view.bounds
imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.view.addSubview(imageView)
Sign up to request clarification or add additional context in comments.

2 Comments

Still looks a little squished
use imageView.contentMode = .scaleAspectFill
3

You have to make your UIImageView frame the size of the area you want to fill. You can do this by setting the frame explicitly or using auto-layout (recommended).

method 1:

let imageView = UIImageView(image: image!)
imageView.frame = self.view.frame;
imageView.contentMode = .scaleAspectFit
//Add Image to view
self.view.addSubview(imageView)

method 2:

let imageView = UIImageView(image: image!)
imageView.contentMode = .scaleAspectFit
//Add Image to view
self.view.addSubview(imageView)
self.view.translatesAutoresizingMaskIntoConstraints = false  

self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[subview]-0-|", options: .directionLeadingToTrailing, metrics: nil, views: ["subview": imageView]))
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[subview]-0-|", options: .directionLeadingToTrailing, metrics: nil, views: ["subview": imageView]))

1 Comment

AutoLayout VFL... old school.
1

You want to manipulate the frame of the UIImageView and not the contentMode.

Try this:

let imageView = UIImageView(image: image!)

imageView.frame = self.view.frame
imageView.contentMode = .scaleToFill

self.view.addSubview(imageView)

3 Comments

It works, but now the image looks squished, I am looking for away to have to full width and aspect the height
If you have this play with imageView.contentMode and look what you want for your needs
Just add imageView.contentMode = .scaleToFill at the code suggested by Teetz

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.