CImageSelectView.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // CImageSelectView.swift
  3. // PDFViewer-Swift
  4. //
  5. // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. import UIKit
  13. @objc protocol CImageSelectViewDelegate: AnyObject {
  14. @objc optional func imageSelectView(_ imageSelectView: CImageSelectView, image: UIImage)
  15. }
  16. class CImageSelectView: UIView, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
  17. weak var delegate: CImageSelectViewDelegate?
  18. weak var parentVC: UIViewController?
  19. private var titleLabel: UILabel?
  20. private var cameraButton: UIButton?
  21. private var photoButton: UIButton?
  22. // MARK: - Initializers
  23. override init(frame: CGRect) {
  24. super.init(frame: frame)
  25. titleLabel = UILabel()
  26. titleLabel?.autoresizingMask = .flexibleRightMargin
  27. titleLabel?.text = NSLocalizedString("Choose Picture", comment: "")
  28. titleLabel?.textColor = .gray
  29. titleLabel?.font = UIFont.systemFont(ofSize: 12.0)
  30. if titleLabel != nil {
  31. addSubview(titleLabel!)
  32. }
  33. cameraButton = UIButton()
  34. cameraButton?.setImage(UIImage(named: "CImageSelectCameraImage", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .normal)
  35. cameraButton?.addTarget(self, action: #selector(buttonItemClicked_camera), for: .touchUpInside)
  36. if cameraButton != nil {
  37. addSubview(self.cameraButton!)
  38. }
  39. photoButton = UIButton()
  40. photoButton?.setImage(UIImage(named: "CImageSelectPhotoImage", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .normal)
  41. photoButton?.addTarget(self, action: #selector(buttonItemClicked_photo), for: .touchUpInside)
  42. if photoButton != nil {
  43. addSubview(self.photoButton!)
  44. }
  45. }
  46. required init?(coder: NSCoder) {
  47. fatalError("init(coder:) has not been implemented")
  48. }
  49. override func layoutSubviews() {
  50. super.layoutSubviews()
  51. titleLabel?.frame = CGRect(x: 20, y: 0, width: 200, height: 30)
  52. cameraButton?.frame = CGRect(x: bounds.size.width - 65, y: 0, width: 45, height: 30)
  53. photoButton?.frame = CGRect(x: bounds.size.width - 110, y: 0, width: 45, height: 30)
  54. }
  55. // MARK: - Action
  56. @objc func buttonItemClicked_camera(_ sender: UIButton) {
  57. cameraButton?.backgroundColor = CPDFColorUtils.CAnnotationBarNoSelectBackgroundColor()
  58. photoButton?.backgroundColor = CPDFColorUtils.CAnnotationBarNoSelectBackgroundColor()
  59. cameraButton?.backgroundColor = CPDFColorUtils.CAnnotationBarSelectBackgroundColor()
  60. let tRootViewControl = parentVC
  61. let imagePickerController = UIImagePickerController()
  62. imagePickerController.delegate = self
  63. imagePickerController.sourceType = .camera
  64. tRootViewControl?.present(imagePickerController, animated: true, completion: nil)
  65. }
  66. @objc func buttonItemClicked_photo(_ sender: UIButton) {
  67. cameraButton?.backgroundColor = CPDFColorUtils.CAnnotationBarNoSelectBackgroundColor()
  68. photoButton?.backgroundColor = CPDFColorUtils.CAnnotationBarNoSelectBackgroundColor()
  69. photoButton?.backgroundColor = CPDFColorUtils.CAnnotationBarSelectBackgroundColor()
  70. let tRootViewControl = parentVC
  71. let imagePickerController = UIImagePickerController()
  72. imagePickerController.delegate = self
  73. imagePickerController.sourceType = .photoLibrary
  74. imagePickerController.allowsEditing = true
  75. imagePickerController.modalPresentationStyle = .popover
  76. if UI_USER_INTERFACE_IDIOM() == .pad {
  77. imagePickerController.popoverPresentationController?.sourceView = self.photoButton
  78. imagePickerController.popoverPresentationController?.sourceRect = ((self.photoButton)?.bounds) ?? .zero
  79. }
  80. tRootViewControl?.present(imagePickerController, animated: true, completion: nil)
  81. }
  82. // MARK: - UIImagePickerControllerDelegate
  83. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
  84. picker.dismiss(animated: true, completion: nil)
  85. var image: UIImage?
  86. if let editedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
  87. image = editedImage
  88. } else if let originalImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
  89. image = originalImage
  90. }
  91. let imageOrientation = image?.imageOrientation
  92. if imageOrientation != .up {
  93. UIGraphicsBeginImageContext((image?.size)!)
  94. image?.draw(in: CGRect(x: 0, y: 0, width: (image?.size.width ?? 0), height: (image?.size.height ?? 0)))
  95. image = UIGraphicsGetImageFromCurrentImageContext()
  96. UIGraphicsEndImageContext()
  97. }
  98. guard let imageData = image!.pngData() else {
  99. return
  100. }
  101. image = UIImage(data: imageData)
  102. let colorMasking: [CGFloat] = [222, 255, 222, 255, 222, 255]
  103. let imageRef = image?.cgImage?.copy(maskingColorComponents: colorMasking)
  104. if let cgImage = imageRef {
  105. image = UIImage(cgImage: cgImage)
  106. }
  107. delegate?.imageSelectView?(self, image: image ?? UIImage())
  108. }
  109. func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
  110. picker.dismiss(animated: true)
  111. }
  112. }