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
  • Please post the code you're using right now to achieve what you want. Dec 27, 2018 at 15:59

1 Answer 1

-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
4
  • 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 Dec 27, 2018 at 18:37
  • For enabling cropping please use let picker = UIImagePickerController() picker.allowsEditing = true and make use of UIImagePickerController.InfoKey.cropRect
    – Bappaditya
    Dec 27, 2018 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 Dec 27, 2018 at 18:53
  • Please refer stackoverflow.com/questions/9041732/…
    – Bappaditya
    Dec 27, 2018 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.