Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cropping an image with imagepickercontroller in swift

Tags:

ios

swift

I am currently making a program in swift that involves a screen of choosing an image from either camera or photo library using action sheet. This is fully functional however I would like to be able to choose a square section from the selected image, similar to apple default apps. How can I implement this? Here is my functional code:

func chooseImage(_ sender: Any) {

    let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self

    let actionSheet = UIAlertController(title: "Photo Source", message: "Choose a source", preferredStyle: .actionSheet)

    actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action:UIAlertAction) in

        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            imagePickerController.sourceType = .camera
            self.present(imagePickerController, animated: true, completion: nil)
        }else{
            print("Camera not available")
        }


    }))

    actionSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (action:UIAlertAction) in
        imagePickerController.sourceType = .photoLibrary
        self.present(imagePickerController, animated: true, completion: nil)
    }))

    actionSheet.addAction(UIAlertAction(title: "Default", style: .default, handler: { (action:UIAlertAction) in
        self.avatarImageView.image = UIImage(named: "Avatar.png")


    }))

    actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

    self.present(actionSheet, animated: true, completion: nil)


}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let image = info[UIImagePickerControllerOriginalImage] as! UIImage

    avatarImageView.image = image

    picker.dismiss(animated: true, completion: nil)
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    picker.dismiss(animated: true, completion: nil)
}
// Saves the User singleton object onto the device
static func saveData() {
    let savedData = NSKeyedArchiver.archivedData(withRootObject: User.sharedUser)
    UserDefaults.standard.set(savedData, forKey: "user")
}
like image 831
Ajp Avatar asked Apr 13 '17 22:04

Ajp


2 Answers

You can use default controls to achieve image cropping.

self.imgPicker.allowsEditing = true

Delegate Method

//MARK: image picker delegate method
    //MARK:
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        var image : UIImage!

        if let img = info[UIImagePickerControllerEditedImage] as? UIImage
        {
            image = img

        }
        else if let img = info[UIImagePickerControllerOriginalImage] as? UIImage
        {
            image = img
        }


        picker.dismiss(animated: true,completion: nil)
 }
like image 64
Jay Mehta Avatar answered Oct 17 '22 13:10

Jay Mehta


Swift 4.0+

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    var image : UIImage!

    if let img = info[UIImagePickerController.InfoKey.editedImage] as? UIImage
    {
        image = img

    }
    else if let img = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
    {
        image = img
    }


    picker.dismiss(animated: true, completion: nil)
}

Don't forget to set allowsEditing to true.

Another option is to use TOCropViewController. Its does more with much less code. What I found good about it that it allows you to change the cropping rectangle.

class ViewController: UIViewController, CropViewControllerDelegate {

    ... //your viewcontroller code

    func presentCropViewController {
        let image: UIImage = ... //Load an image

        let cropViewController = CropViewController(image: image)
        cropViewController.delegate = self
        present(cropViewController, animated: true, completion: nil)
    }

    func cropViewController(_ cropViewController: CropViewController, 
         didCropToImage image: UIImage, withRect cropRect: CGRect, angle: Int) {
        // 'image' is the newly cropped version of the original image
    }
}
like image 9
ARR Avatar answered Oct 17 '22 13:10

ARR