裁剪后UIImage会旋转

时间:2017-03-05 16:29:09

标签: swift uiimage uipickerview

我有一个类设置,允许用户从库中添加图像,裁剪并保存。

设置代码,以便如果检索到的图像是纵向的,则在裁剪之前会出现一个纵向形状的边框,如果是横向,则会出现景观边框。

如果选择的图像是规则形状的图像,则一切正常。但是,如果检索到的图像是纵向而不是常规比例(意味着更接近正方形而实际上不是正方形),则图像在裁剪后旋转。似乎系统认为它就像风景图像一样。

以下是作物之前和之后的示例。即使我放大并使图像覆盖整个屏幕,它也会旋转图像:

enter image description here

enter image description here

import Foundation
import UIKit

class SelectImageViewController: UIViewController, UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIScrollViewDelegate{


    @IBOutlet weak var imageView: UIImageView!

    @IBOutlet weak var imageConstraintTop: NSLayoutConstraint!
    @IBOutlet weak var imageConstraintRight: NSLayoutConstraint!
    @IBOutlet weak var imageConstraintLeft: NSLayoutConstraint!
    @IBOutlet weak var imageConstraintBottom: NSLayoutConstraint!

    var lastZoomScale: CGFloat = -1
    var imageName: String = ""
    var userPhotoUUID = UUID().uuidString
    let userDefault = UserDefaults.standard
    var userDatabase: UserDatabase = UserDatabase()
    let picker = UIImagePickerController()


    @IBOutlet var scrollView: UIScrollView!{
        didSet{
            scrollView.delegate = self
            scrollView.minimumZoomScale = 1.0
            scrollView.maximumZoomScale = 5.0
        }
    }

    @IBOutlet weak var ratioSelector: UISegmentedControl!

    @IBOutlet var cropAreaViewL: CropAreaViewL!

    var cropAreaL:CGRect{
        get{
            let factor = imageView.image!.size.width/view.frame.width
            let scale = 1/scrollView.zoomScale
            let imageFrame = imageView.imageFrame()
            let x = (scrollView.contentOffset.x + cropAreaViewL.frame.origin.x - imageFrame.origin.x) * scale * factor
            let y = (scrollView.contentOffset.y + cropAreaViewL.frame.origin.y - imageFrame.origin.y) * scale * factor
            let width = cropAreaViewL.frame.size.width * scale * factor
            let height = cropAreaViewL.frame.size.height * scale * factor
            return CGRect(x: x, y: y, width: width, height: height)
        }
    }

    @IBOutlet var cropAreaViewP: CropAreaViewP!

    var cropAreaP:CGRect{
        get{
            let factor = imageView.image!.size.height/view.frame.height
            let scale = 1/scrollView.zoomScale
            let imageFrame = imageView.imageFrame()
            let x = (scrollView.contentOffset.x + cropAreaViewP.frame.origin.x - imageFrame.origin.x) * scale * factor
            let y = (scrollView.contentOffset.y + cropAreaViewP.frame.origin.y - imageFrame.origin.y) * scale * factor
            let width = cropAreaViewP.frame.size.width * scale * factor
            let height = cropAreaViewP.frame.size.height * scale * factor
            return CGRect(x: x, y: y, width: width, height: height)
        }
    }


    fileprivate var speciePhotos: Array<SpeciePhotoModel> = [SpeciePhotoModel]()


    func randomNumber(range: ClosedRange<Int> = 30000...99998) -> Int {
        let min = range.lowerBound
        let max = range.upperBound
        return Int(arc4random_uniform(UInt32(1 + max - min))) + min
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "+", style: .plain, target: self, action: #selector(SelectImageViewController.add(_:)))

        let id = randomNumber()
        userDefault.set(id, forKey: "photoID")

        self.cropAreaViewP.isHidden = true
        self.cropAreaViewL.isHidden = true
        self.cropAreaViewL.layer.borderColor = (UIColor.red).cgColor
        self.cropAreaViewL.layer.borderWidth = 1.0
        self.cropAreaViewP.layer.borderColor = (UIColor.red).cgColor
        self.cropAreaViewP.layer.borderWidth = 1.0
        self.add.layer.cornerRadius = 6.0
        self.ratioSelector.layer.cornerRadius = 6.0
        self.tabBarController?.tabBar.isHidden = true
        self.add.isHidden = true
        self.ratioSelector.isHidden = true

        updateZoom()

    }


    func updateConstraints() {
        if let image = imageView.image {
            let imageWidth = image.size.width
            let imageHeight = image.size.height

            let viewWidth = scrollView.bounds.size.width
            let viewHeight = scrollView.bounds.size.height

            // center image if it is smaller than the scroll view
            var hPadding = (viewWidth - scrollView.zoomScale * imageWidth) / 2
            if hPadding < 0 { hPadding = 0 }

            var vPadding = (viewHeight - scrollView.zoomScale * imageHeight) / 2
            if vPadding < 0 { vPadding = 0 }

            imageConstraintLeft.constant = hPadding
            imageConstraintRight.constant = hPadding

            imageConstraintTop.constant = vPadding
            imageConstraintBottom.constant = vPadding

            view.layoutIfNeeded()
        }
    }

    fileprivate func updateZoom() {
        if let image = imageView.image {
            var minZoom = min(scrollView.bounds.size.width / image.size.width,
                              scrollView.bounds.size.height / image.size.height)

            if minZoom > 1 { minZoom = 1 }

            scrollView.minimumZoomScale = 0.3 * minZoom

            // Force scrollViewDidZoom fire if zoom did not change
            if minZoom == lastZoomScale { minZoom += 0.000001 }

            scrollView.zoomScale = minZoom
            lastZoomScale = minZoom
        }
    }


    @IBAction func ratioSelector(_ sender: AnyObject) {

        switch ratioSelector.selectedSegmentIndex
        {
        case 0:// Landscape

            self.cropAreaViewP.isHidden = true
            self.cropAreaViewL.isHidden = false

        case 1: // Portrait

            self.cropAreaViewL.isHidden = true
            self.cropAreaViewP.isHidden = false

        default:
            break; 
        } 
    }


    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }

    @IBOutlet weak var add : UIButton!


    @IBAction func add(_ sender: UIButton) {

        imageView.image = nil

        let picker = UIImagePickerController()
        picker.delegate = self
        picker.sourceType = .photoLibrary
        picker.allowsEditing = false
        self.present(picker, animated: true, completion: nil)

        self.ratioSelector.isHidden = false
        self.add.isHidden = false

        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Crop", style: .plain, target: self, action: #selector(SelectImageViewController.crop(_:)))

    }

    @IBAction func change(_ sender: UIButton) {

        imageView.image = nil

        let picker = UIImagePickerController()
        picker.delegate = self
        picker.sourceType = .photoLibrary
        picker.allowsEditing = false
        self.present(picker, animated: true, completion: nil)

        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Crop", style: .plain, target: self, action: #selector(SelectImageViewController.crop(_:)))

    }


    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage

        if chosenImage.size.height > chosenImage.size.width
        {
            self.cropAreaViewL.isHidden = true
            self.cropAreaViewP.isHidden = false
            self.ratioSelector.selectedSegmentIndex = 1
            imageView.image = chosenImage
        }
        else
        {
            self.cropAreaViewP.isHidden = true
            self.cropAreaViewL.isHidden = false
            self.ratioSelector.selectedSegmentIndex = 0
            imageView.image = chosenImage
        }
        self.dismiss(animated: true, completion: nil)
    }


    @IBAction func crop(_ sender: UIButton) {

        if cropAreaViewP.isHidden == true {
            self.cropAreaViewL.layer.borderColor = (UIColor.clear).cgColor
            let croppedCGImage = imageView.image?.cgImage?.cropping(to: cropAreaL)
            let croppedImage = UIImage(cgImage: croppedCGImage!)
            imageView.image = croppedImage
            scrollView.zoomScale = 1
        } else {
            self.cropAreaViewP.layer.borderColor = (UIColor.clear).cgColor
            let croppedCGImage = imageView.image?.cgImage?.cropping(to: cropAreaP)
            let croppedImage = UIImage(cgImage: croppedCGImage!)
            imageView.image = croppedImage
            scrollView.zoomScale = 1
        }

        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Next", style: .plain, target: self, action: #selector(SelectImageViewController.saveButtonAction(_:)))


    }
}

extension UIImageView{
    func imageFrame()->CGRect{
        let imageViewSize = self.frame.size
        guard let imageSize = self.image?.size else{return CGRect.zero}
        let imageRatio = imageSize.width / imageSize.height
        let imageViewRatio = imageViewSize.width / imageViewSize.height

        if imageRatio < imageViewRatio { // Portrait
            let scaleFactor = imageViewSize.height / imageSize.height
            let width = imageSize.width * scaleFactor
            let topLeftX = (imageViewSize.width - width) * 0.5
            return CGRect(x: topLeftX, y: 0, width: width, height: imageViewSize.height)
        }else{  // Landscape
            let scaleFactor = imageViewSize.width / imageSize.width
            let height = imageSize.height * scaleFactor
            let topLeftY = (imageViewSize.height - height) * 0.5
            return CGRect(x: 0, y: topLeftY, width: imageViewSize.width, height: height)
        }
    }
}


class CropAreaViewL: UIView {

    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        return false
    }
}

class CropAreaViewP: UIView {

    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        return false
    }

}

任何帮助都会很大。

1 个答案:

答案 0 :(得分:1)

您可以使用max方法确保没有得到低于零的值:

let vPadding = max((viewHeight - scrollView.zoomScale * imageHeight) / 2, 0)

如果你需要使你的图像平方,你可以按照以下方式进行:

extension UIImage {

    var isPortrait:  Bool    { return size.height > size.width }
    var isLandscape: Bool    { return size.width > size.height }
    var breadth:     CGFloat { return min(size.width, size.height) }
    var breadthSize: CGSize  { return CGSize(width: breadth, height: breadth) }
    var squared: UIImage? {
        guard let cgImage = cgImage?.cropping(to:
            CGRect(origin: CGPoint(x: isLandscape ? floor((size.width-size.height)/2) : 0, y: isPortrait  ? floor((size.height-size.width)/2) : 0),
                   size: breadthSize)) else { return nil }
        return UIImage(cgImage: cgImage)
    }
}

要修复您需要重新绘制图片的方向问题,您可以使用此answer中的展平属性。

游乐场:

let profilePicture = UIImage(data: try! Data(contentsOf: URL(string:"https://i.stack.imgur.com/Xs4RX.jpg")!))!
if let squared = profilePicture.squared {
    squared

}