PhotoEditor+Controls.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // PhotoEditor+Controls.swift
  3. // Pods
  4. //
  5. // Created by Mohamed Hamed on 6/16/17.
  6. //
  7. //
  8. import Foundation
  9. import UIKit
  10. // MARK: - Control
  11. public enum control {
  12. case crop
  13. case sticker
  14. case draw
  15. case text
  16. case save
  17. case share
  18. case clear
  19. }
  20. extension PhotoEditorViewController {
  21. //MARK: Top Toolbar
  22. @IBAction func cancelButtonTapped(_ sender: Any) {
  23. photoEditorDelegate?.canceledEditing()
  24. self.dismiss(animated: true, completion: nil)
  25. }
  26. @IBAction func cropButtonTapped(_ sender: UIButton) {
  27. let controller = CropViewController()
  28. controller.delegate = self
  29. controller.image = image
  30. let navController = UINavigationController(rootViewController: controller)
  31. present(navController, animated: true, completion: nil)
  32. }
  33. @IBAction func stickersButtonTapped(_ sender: Any) {
  34. addStickersViewController()
  35. }
  36. @IBAction func drawButtonTapped(_ sender: Any) {
  37. isDrawing = true
  38. canvasImageView.isUserInteractionEnabled = false
  39. doneButton.isHidden = false
  40. colorPickerView.isHidden = false
  41. hideToolbar(hide: true)
  42. }
  43. @IBAction func textButtonTapped(_ sender: Any) {
  44. isTyping = true
  45. let textView = UITextView(frame: CGRect(x: 0, y: canvasImageView.center.y,
  46. width: UIScreen.main.bounds.width, height: 30))
  47. textView.textAlignment = .center
  48. textView.font = UIFont(name: "Helvetica", size: 30)
  49. textView.textColor = textColor
  50. textView.layer.shadowColor = UIColor.black.cgColor
  51. textView.layer.shadowOffset = CGSize(width: 1.0, height: 0.0)
  52. textView.layer.shadowOpacity = 0.2
  53. textView.layer.shadowRadius = 1.0
  54. textView.layer.backgroundColor = UIColor.clear.cgColor
  55. textView.autocorrectionType = .no
  56. textView.isScrollEnabled = false
  57. textView.delegate = self
  58. self.canvasImageView.addSubview(textView)
  59. addGestures(view: textView)
  60. textView.becomeFirstResponder()
  61. }
  62. @IBAction func doneButtonTapped(_ sender: Any) {
  63. view.endEditing(true)
  64. doneButton.isHidden = true
  65. colorPickerView.isHidden = true
  66. canvasImageView.isUserInteractionEnabled = true
  67. hideToolbar(hide: false)
  68. isDrawing = false
  69. }
  70. //MARK: Bottom Toolbar
  71. @IBAction func saveButtonTapped(_ sender: AnyObject) {
  72. UIImageWriteToSavedPhotosAlbum(canvasView.toImage(),self, #selector(PhotoEditorViewController.image(_:withPotentialError:contextInfo:)), nil)
  73. }
  74. @IBAction func shareButtonTapped(_ sender: UIButton) {
  75. let activity = UIActivityViewController(activityItems: [canvasView.toImage()], applicationActivities: nil)
  76. present(activity, animated: true, completion: nil)
  77. }
  78. @IBAction func clearButtonTapped(_ sender: AnyObject) {
  79. //clear drawing
  80. canvasImageView.image = nil
  81. //clear stickers and textviews
  82. for subview in canvasImageView.subviews {
  83. subview.removeFromSuperview()
  84. }
  85. }
  86. @IBAction func continueButtonPressed(_ sender: Any) {
  87. let img = self.canvasView.toImage()
  88. photoEditorDelegate?.doneEditing(image: img)
  89. self.dismiss(animated: true, completion: nil)
  90. }
  91. //MAKR: helper methods
  92. @objc func image(_ image: UIImage, withPotentialError error: NSErrorPointer, contextInfo: UnsafeRawPointer) {
  93. let alert = UIAlertController(title: "Image Saved", message: "Image successfully saved to Photos library", preferredStyle: UIAlertController.Style.alert)
  94. alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
  95. self.present(alert, animated: true, completion: nil)
  96. }
  97. func hideControls() {
  98. for control in hiddenControls {
  99. switch control {
  100. case .clear:
  101. clearButton.isHidden = true
  102. case .crop:
  103. cropButton.isHidden = true
  104. case .draw:
  105. drawButton.isHidden = true
  106. case .save:
  107. saveButton.isHidden = true
  108. case .share:
  109. shareButton.isHidden = true
  110. case .sticker:
  111. stickerButton.isHidden = true
  112. case .text:
  113. stickerButton.isHidden = true
  114. }
  115. }
  116. }
  117. }