1

image showing the imagepickercontroller

As shown in this figure, that square position is not changing or resizing when i take picture from camera but it is working from gallery. Please help to how to change the position of crop square to go to required part of image.

the code used is given below

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

    if let  image = info[UIImagePickerControllerOriginalImage] as? UIImage
    {
        print(image.size)
        let croppedImage = info[UIImagePickerControllerEditedImage] as? UIImage
        print(croppedImage ?? "")
        let cropRect = (info[UIImagePickerControllerCropRect]! as AnyObject).cgRectValue
        print(cropRect?.size ?? "")
        let _ = UIImage.cropImage(image: image, toRect: CGRect.init(x: 0, y: 0, w: self.imgProfile.bounds.width, h: self.imgProfile.bounds.width))
        print(image.size)
        self.imgProfile.image = croppedImage
        profileImgUpdated = true

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

func openCamera(){
    self.imagePicker.sourceType = .camera
    self.imagePicker.allowsEditing = true
    self.imagePicker.delegate = self
    self.present(imagePicker, animated: true)
}

func openPhotoLibrary() {
    self.imagePicker.sourceType = .photoLibrary
    self.imagePicker.allowsEditing = true
    self.imagePicker.delegate = self
    self.present(imagePicker, animated: true)
}

1 Answers1

-1

In Swift 4.2 didFinishPickingMediaWithInfo info: [String : Any] is depricated, try with,

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
    if let editedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
        self.imgProfile.image = editedImage
    } else if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
        self.imgProfile.image = image
    }
    self.dismiss(animated: true, completion: nil)
}

To enable editing,

let picker = UIImagePickerController()
picker.allowsEditing = true
Bappaditya
  • 9,494
  • 3
  • 20
  • 29
  • 1
    this is not helpful i was asking for solution for the cropped square in image to move but it wasnt moving when i am taking picture through camera – sandeep kunsoth Dec 27 '18 at 18:37
  • For enabling cropping please use `let picker = UIImagePickerController() picker.allowsEditing = true` and make use of `UIImagePickerController.InfoKey.cropRect` – Bappaditya Dec 27 '18 at 18:45
  • I have cropping but as u see in above image I am not able to change crop square to other part of image – sandeep kunsoth Dec 27 '18 at 18:53
  • Please refer https://stackoverflow.com/questions/9041732/set-dimensions-for-uiimagepickercontroller-move-and-scale-cropbox/9101576#9101576 – Bappaditya Dec 27 '18 at 18:59