Hello guys, Today we will learn about how to “Cropping Image In Swift” IOS.
Overview Of Cropping Image :-
In this blog, we are going to implement the functionality of “Cropping Image”.
when we use an image picker we only take selected images we couldn’t crop the image according to our need and we couldn’t rotate our image. To overcome this problem we use the crop image functionality.
For this functionality, we need to download a pod file
1 |
pod 'QCropper' |
How to install pod file – we need to follow some steps
- open your xCode project in terminal
- write pod init -> Enter
- writre pod install -> Enter
- wrtie pod file name like below image
5. write pod install -> Enter
Now open your .xcworkspace project into your Xcode
Now open viewController Class and write some code for upload images from the gallery.
you need to call the “import QCropper” module in the top
1 2 3 4 5 |
let picker = UIImagePickerController() picker.sourceType = .photoLibrary picker.allowsEditing = false picker.delegate = self self.present(picker, animated: true, completion: nil) |
After that, you need to call the image Picker method
1 2 3 4 5 6 |
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) { if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage { print(image) } } |
when the image picker method is called we need to call crop Image Functionality inside the image picker controller method
1 2 3 4 5 6 7 8 |
let cropper = CropperViewController(originalImage: image) cropper.delegate = self picker.dismiss(animated: true) { self.present(cropper, animated: true, completion: nil) } |
After that, we need to call the delegate function of cropper Image functionality
1 2 3 4 5 6 7 8 9 10 11 |
func cropperDidConfirm(_ cropper: CropperViewController, state: CropperState?) { cropper.dismiss(animated: true, completion: nil) if let state = state, let image = cropper.originalImage.cropped(withCropperState: state) { self.cropImageView.image = image } else { print("Something went wrong") } self.dismiss(animated: true, completion: nil) } |
Here is the full code of the functionality
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import UIKit import QCropper class ViewController: UIViewController { @IBOutlet weak var uploadImageBtnOutlet: UIButton! @IBOutlet weak var cropImageView: UIImageView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } @IBAction func uploadImagetap(_ sender: Any) { let picker = UIImagePickerController() picker.sourceType = .photoLibrary picker.allowsEditing = false picker.delegate = self self.present(picker, animated: true, completion: nil) } } extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate { func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) { if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage { print(image) let cropper = CropperViewController(originalImage: image) cropper.delegate = self picker.dismiss(animated: true) { self.present(cropper, animated: true, completion: nil) } } } } extension ViewController: CropperViewControllerDelegate { func cropperDidConfirm(_ cropper: CropperViewController, state: CropperState?) { cropper.dismiss(animated: true, completion: nil) if let state = state, let image = cropper.originalImage.cropped(withCropperState: state) { self.cropImageView.image = image } else { print("Something went wrong") } self.dismiss(animated: true, completion: nil) } } |
Now run your code:- Input
Conclusion:-
In this blog, we discussed how to crop the image
I hope this blog will help you to get about Cropping Image In Swift functionality
Thanks for reading!!