如何在 Xcode swift 中更改图片方向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43482953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:55:47  来源:igfitidea点击:

How to change picture orientation in Xcode swift

swiftxcodeimagelandscape

提问by Gurpinder Singh

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

我从照片库中获取一张图片,并使用以下代码将其称为 image:

@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.

我想看看图像是横向还是纵向。然后,如果是横向,我想将其更改为纵向。

回答by Malik

You can get the orientation by using image.imageOrientation.

您可以使用image.imageOrientation.

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

为了改变方向,最快的解决方案是从实际图像创建一个新的 UIImage 如下:

let newImage = UIImage(cgImage: image.cgImage!, scale: image.scale, orientation: .up)