You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.7 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. //
  2. // ImageCroper.swift
  3. //
  4. //
  5. // Created by shishir sapkota
  6. //
  7. import Foundation
  8. import RSKImageCropper
  9. protocol ImageCropperDelegate {
  10. func didCropWith(image: UIImage)
  11. func didFailedCropWith(error: Error)
  12. }
  13. class ImageCroper: NSObject {
  14. var imageCropper: RSKImageCropViewController = RSKImageCropViewController()
  15. weak var presentingViewController: UIViewController?
  16. var delegate: ImageCropperDelegate?
  17. fileprivate var cropRatio: CGFloat = 1
  18. init(presentingViewController: UIViewController) {
  19. super.init()
  20. self.presentingViewController = presentingViewController
  21. self.imageCropper.delegate = self
  22. self.imageCropper.dataSource = self
  23. }
  24. func cropImage(image: UIImage, ratio: CGFloat = 1) {
  25. self.cropRatio = ratio
  26. self.imageCropper = RSKImageCropViewController(image: image, cropMode: .custom)
  27. imageCropper.chooseButton.setTitle("done_text", for: .normal)
  28. self.imageCropper.delegate = self
  29. self.imageCropper.dataSource = self
  30. self.presentingViewController?.present(imageCropper, animated: true, completion: nil)
  31. }
  32. }
  33. extension ImageCroper: RSKImageCropViewControllerDelegate {
  34. func imageCropViewControllerDidCancelCrop(_ controller: RSKImageCropViewController) {
  35. presentingViewController?.dismiss(animated: true, completion: nil)
  36. }
  37. func imageCropViewController(_ controller: RSKImageCropViewController, didCropImage croppedImage: UIImage, usingCropRect cropRect: CGRect) {
  38. presentingViewController?.dismiss(animated: true, completion: {
  39. // image cropped here
  40. self.delegate?.didCropWith(image: croppedImage)
  41. })
  42. }
  43. func imageCropViewController(_ controller: RSKImageCropViewController, didCropImage croppedImage: UIImage, usingCropRect cropRect: CGRect, rotationAngle: CGFloat) {
  44. self.delegate?.didCropWith(image: croppedImage)
  45. self.imageCropper.dismiss(animated: true, completion: nil)
  46. }
  47. }
  48. extension ImageCroper: RSKImageCropViewControllerDataSource {
  49. func imageCropViewControllerCustomMaskRect(_ controller: RSKImageCropViewController) -> CGRect {
  50. guard let viewController = presentingViewController else {return CGRect.zero}
  51. var maskSize:CGSize
  52. let heightRatio: CGFloat = self.cropRatio //35/71
  53. let maskWidth:CGFloat = UIScreen.main.bounds.size.width - 40
  54. let maskHeight: CGFloat = CGFloat( ceilf( Float(maskWidth * heightRatio)))
  55. maskSize = CGSize(width: maskWidth, height: maskHeight)
  56. let maskRect: CGRect = CGRect(x: (viewController.view.frame.size.width - maskSize.width ) * 0.5, y: (viewController.view.frame.size.height - maskSize.height) * 0.5, width: maskSize.width, height: maskSize.height)
  57. return maskRect
  58. }
  59. func imageCropViewControllerCustomMaskPath(_ controller: RSKImageCropViewController) -> UIBezierPath {
  60. let rect = controller.maskRect;
  61. let point1 = CGPoint(x: rect.minX,y: rect.minY);
  62. let point2 = CGPoint(x: rect.maxX, y: rect.minY);
  63. let point3 = CGPoint(x: rect.maxX, y: rect.maxY);
  64. let point4 = CGPoint(x: rect.minX, y: rect.maxY);
  65. let rectangle = UIBezierPath()
  66. rectangle.move(to: point1)
  67. rectangle.addLine(to: point2)
  68. rectangle.addLine(to: point3)
  69. rectangle.addLine(to: point4)
  70. rectangle.close()
  71. return rectangle;
  72. }
  73. func imageCropViewControllerCustomMovementRect(_ controller: RSKImageCropViewController) -> CGRect {
  74. return controller.maskRect
  75. }
  76. }