2

I get a picture from the photo library and call it image using this code:

@IBAction func importImage(_ sender: AnyObject) {
    let image = UIImagePickerController()
    image.delegate = self
    image.sourceType = UIImagePickerControllerSourceType.photoLibrary
    image.allowsEditing = false
    self.present(image, animated: true) { }
}

and this code:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        myImageView.image = image
        dismiss(animated: true, completion: nil)
        SubmitBtnOutlet.isHidden = false;
        Gurpcontroller.person = "yours";
        Gurpcontroller.collecterthing = "Coin";
        PictureViewController.imageuniversal = myImageView
    } else {
        //Error message
    }
}

I want to see if image is landscape or portrait. Then, if it is landscape I want to change it to portrait.

3
  • By definition, landscape means the image size is more width than height. So if an image is 200 width by 100 height, could you include more details on what you want? If your UIImageView is 200 by 200, you can always use .scaleAspectFit, but it's difficult to know if that is what you are trying to do. Commented Apr 20, 2017 at 22:50
  • Polished code syntax a bit to be as concise as possible and remove empty space. Also changed title wording slightly (orientate is not a word, but is represented by the phrase "change orientation" the way I understood the author). I also fixed minor spelling of portrait, to clarify the question as an image orientation question to always get the image to portrait mode. Commented Apr 22, 2017 at 12:10
  • In terms of the answer, the first comment by @dfd is spot on -- if an image's width is larger than it's height, it is by definition in landscape mode. From there, you can rotate or crop to your heart's content to recover a portrait image. Commented Apr 22, 2017 at 12:11

1 Answer 1

15

You can get the orientation by using image.imageOrientation.

In order to change the orientation, the quickest solution would be to create a new UIImage from the actual image as follows:

let newImage = UIImage(cgImage: image.cgImage!, scale: image.scale, orientation: .up)
Sign up to request clarification or add additional context in comments.

3 Comments

It wont let me do it this is what i put var newImage = PictureViewController.imageuniversal.image newImage = UIImage(cgnewImage: newImage?.cgImage!, scale: newImage?.scale, orientation: .up)
Please read the answer properly. The variable image is the image you want to manipulate. The variable newImage is the one which will store the resulting image. cgImage is the parameter name, you cannot change it to cgnewImage. Also you are using newImage's properties on the right hand side. Basically you are trying to access the properties of an image that doesn't exist yet.
Use these lines of code instead let image = PictureViewController.imageuniversal.image let newImage = UIImage(cgImage: image.cgImage!, scale: image.scale, orientation: .up)

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.