如何在Swift中CROP图片?

问题描述 投票:0回答:1

你好,我正在使用UIImagePicker来拍摄图片,但是在挑选图片的时候,我想把图片CROP。如何做到这一点

我可以从图库中选择图片到imageview,但在选择图片时,我需要裁剪图片。

我已经尝试了以下代码。

 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)
}

}

就像这样,我得到的是... ...没有裁剪。

enter image description here

在这里,我得到的图像从画廊到imageview,但没有得到裁剪。

如何裁剪图像,请帮助我的代码。

ios swift image camera crop
1个回答
0
投票

我检查了你的代码,我发现你没有设置 delegateUIImagePickerController

我试着用 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)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.