PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 27, 2022

[FIXED] How to CROP Images in Swift?

 July 27, 2022     camera, crop, image, ios, swift     No comments   

Issue

Hello i am using UIImagePicker to take images but while picking images i want to CROP that image.. How to do that

i am able to pick images from gallery to imageview but while selecting image i need to crop the image.

I have tried below code:

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

}

like this i am getting.. no crop

enter image description here

here i am getting images from gallery to imageview but not getting crop.

How to crop the images, please help me with the code.


Solution

I check with your code and i found you are missing to set delegate of UIImagePickerController

I try with photoLibrary for now

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


Answered By - Rohit Makwana
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing