如何裁剪 Swift 中的图像?

How to CROP Images in Swift?

你好,我正在使用 UIImagePicker 拍摄图像,但在选择图像时我想裁剪该图像。如何做到这一点

我可以从图库中选择图像到图像视图,但是在选择图像时我需要裁剪图像。

我试过下面的代码:

 class AddNewEventViewController: UIViewController, UIPickerViewDelegate,UIImagePickerControllerDelegate, UIPickerViewDataSource,UINavigationControllerDelegate {

@IBOutlet weak var imgPick: UIImageView!

var picker = UIImagePickerController()

 override func viewDidLoad() {
    super.viewDidLoad()
    hideKeyboardWhenTappedAround()
    picker.delegate = self
    picker.allowsEditing = true
}
  @IBAction func addProfileBtnAction(_ sender: Any) {
    let actionSheetController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
    actionSheetController.popoverPresentationController?.sourceView = self.contentView
    let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in
    }
    actionSheetController.addAction(cancelAction)
    let takePictureAction: UIAlertAction = UIAlertAction(title: "TakePhoto", style: .default) { action -> Void in
        self.openCameraPicker()
    }
    actionSheetController.addAction(takePictureAction)
    let choosePictureAction: UIAlertAction = UIAlertAction(title: "ChooseFromLibrary", style: .default) { action -> Void in
        self.openPhotoGallery()
    }
    actionSheetController.addAction(choosePictureAction)
    //Present the
    self.present(actionSheetController, animated: true, completion: nil)
}
func openCameraPicker() {
    picker.sourceType = UIImagePickerController.SourceType.camera
    picker.cameraCaptureMode = .photo
    picker.allowsEditing = true
    picker.modalPresentationStyle = .fullScreen
    present(picker,animated: true,completion: nil)
}
func openPhotoGallery() {
    picker.sourceType = .photoLibrary
    picker.allowsEditing = true

    picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
    present(picker, animated: true, completion: nil)
}
// MARK: - UIImagePickerControllerDelegate Methods
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let img = info[UIImagePickerController.InfoKey.editedImage] as? UIImage{
        imgPick.image = img
    }
    else if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
        self.imgPick.image = image
    }
    dismiss(animated: true, completion: nil)
}

}

像这样我得到..没有裁剪

这里我正在从画廊获取图像到图像视图,但没有裁剪。

如何裁剪图片,请帮我看代码

我检查了您的代码,发现您缺少设置 delegate of UIImagePickerController

我现在试试 photoLibrary

class ImagePickerViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet var imageView:UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.openPhotoGallery()
    }

    func openPhotoGallery() {

        let picker = UIImagePickerController()
        picker.delegate = self
        picker.sourceType = .photoLibrary
        picker.allowsEditing = true
        picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
        present(picker, animated: true, completion: nil)
    }

    // MARK: - UIImagePickerControllerDelegate Methods
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

        if let img = info[UIImagePickerController.InfoKey.editedImage] as? UIImage{
            imageView.image = img
        }
        dismiss(animated: true, completion: nil)
    }
}