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.

113 lines
3.7 KiB

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. import Localize_Swift
  10. protocol ImageCropperDelegate {
  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. 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. self.imageCropper.delegate = self
  30. self.imageCropper.dataSource = self
  31. self.presentingViewController?.present(imageCropper, animated: true, completion: nil)
  32. }
  33. }
  34. extension ImageCroper: RSKImageCropViewControllerDelegate {
  35. func imageCropViewControllerDidCancelCrop(_ controller: RSKImageCropViewController) {
  36. presentingViewController?.dismiss(animated: true, completion: nil)
  37. }
  38. func imageCropViewController(_ controller: RSKImageCropViewController, didCropImage croppedImage: UIImage, usingCropRect cropRect: CGRect) {
  39. presentingViewController?.dismiss(animated: true, completion: {
  40. // image cropped here
  41. self.delegate?.didCropWith(image: croppedImage)
  42. })
  43. }
  44. func imageCropViewController(_ controller: RSKImageCropViewController, didCropImage croppedImage: UIImage, usingCropRect cropRect: CGRect, rotationAngle: CGFloat) {
  45. self.delegate?.didCropWith(image: croppedImage)
  46. self.imageCropper.dismiss(animated: true, completion: nil)
  47. }
  48. }
  49. extension ImageCroper: RSKImageCropViewControllerDataSource {
  50. func imageCropViewControllerCustomMaskRect(_ controller: RSKImageCropViewController) -> CGRect {
  51. guard let viewController = presentingViewController else {return CGRect.zero}
  52. var maskSize:CGSize
  53. let heightRatio: CGFloat = self.cropRatio //35/71
  54. let maskWidth:CGFloat = UIScreen.main.bounds.size.width - 40
  55. let maskHeight: CGFloat = CGFloat( ceilf( Float(maskWidth * heightRatio)))
  56. maskSize = CGSize(width: maskWidth, height: maskHeight)
  57. 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)
  58. return maskRect
  59. }
  60. func imageCropViewControllerCustomMaskPath(_ controller: RSKImageCropViewController) -> UIBezierPath {
  61. let rect = controller.maskRect;
  62. let point1 = CGPoint(x: rect.minX,y: rect.minY);
  63. let point2 = CGPoint(x: rect.maxX, y: rect.minY);
  64. let point3 = CGPoint(x: rect.maxX, y: rect.maxY);
  65. let point4 = CGPoint(x: rect.minX, y: rect.maxY);
  66. let rectangle = UIBezierPath()
  67. rectangle.move(to: point1)
  68. rectangle.addLine(to: point2)
  69. rectangle.addLine(to: point3)
  70. rectangle.addLine(to: point4)
  71. rectangle.close()
  72. return rectangle;
  73. }
  74. func imageCropViewControllerCustomMovementRect(_ controller: RSKImageCropViewController) -> CGRect {
  75. return controller.maskRect
  76. }
  77. }