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.

120 lines
3.6 KiB

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