TonyW
TonyW

Reputation: 18875

Swift in iOS: Resizing UIImage for Portrait or Landscape

I am trying to create an app that allows users to select pictures from their photo library on their iPhone and I would like to make the UIImage view more responsive to either Portrait or Landscape view of the screen.

In the view controller, I have a UIImage: UIImage in the ViewController

then I set four constraints on it Constraints

However, when I select an image from the photo library, the size of the picture is not responsive to Portrait or Landscape. The size of the picture seems much smaller than the width (380) & height (419) of the UIImage. Landscape view

The relevant code to the UIImagePickerController is:

   self.pickedImage.image = image
    self.pickedImage.contentMode = .ScaleAspectFit

So I am wondering how to make the size of the picture responsive to the screen (landscape/portrait). Thanks

Upvotes: 0

Views: 2523

Answers (2)

Baptiste Bouvier
Baptiste Bouvier

Reputation: 69

You're very close - either:

  1. self.pickedImage.contentMode = .ScaleAspectFill
  2. self.pickedImage.contentMode = .ScaleToFill

I personally prefer the top one, but both should do the trick.

Make sure you have the image view taking up the whole screen in the storyboard file before applying those constraints too.

Hope this helps!

Upvotes: 1

Glorfindel
Glorfindel

Reputation: 22631

It is doing exactly what you ordered it to: a top spacing of 16 pixels to the navigation bar and a bottom spacing of 99 pixels to the toolbar. The UIImageView is much wider than the image itself (you can check this by changing its background color). If you make those margins smaller, you will see that the image will increase in size.

Upvotes: 1

Related Questions