【问题标题】:How to CROP Images in Swift?如何在 Swift 中裁剪图像?
【发布时间】:2020-08-14 14:36:26
【问题描述】:

您好,我正在使用 UIImagePicker 拍摄图像,但是在选择图像时我想裁剪该图像.. 怎么做

我可以从图库中选择图像到 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)
}

}

像这样我得到..没有作物

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

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

【问题讨论】:

    标签: ios swift image camera crop


    【解决方案1】:

    我检查了您的代码,发现您缺少设置 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)
        }
    }
    

    【讨论】:

    • 我还没有添加该代码.. 但我 picker.delegate = self 仍然无法裁剪...现在我也编辑了帖子
    • 添加到你的 VC -> UIImagePickerControllerDelegate, UINavigationControllerDelegate ?
    • 请检查我编辑过的问题.. 刚刚编辑过 aginn.. 我错过了什么
    • 你可以裁剪图像..请需要代码..任何工作代码..或工作示例都会有所帮助
    • 请发布您的整个代码,或者如果您在 github 中有示例项目请分享..我使用的是 Xcode 11.3 版本..我需要代码请分享
    猜你喜欢
    • 2016-06-16
    • 2017-04-12
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 2015-02-02
    • 2012-11-16
    • 2021-07-24
    • 2021-06-24
    相关资源
    最近更新 更多