NCViewerImageZoom.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // NCViewerImageZoom.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 24/10/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. class NCViewerImageZoom: UIViewController {
  25. @IBOutlet weak var imageViewBottomConstraint: NSLayoutConstraint!
  26. @IBOutlet weak var imageViewLeadingConstraint: NSLayoutConstraint!
  27. @IBOutlet weak var imageViewTopConstraint: NSLayoutConstraint!
  28. @IBOutlet weak var imageViewTrailingConstraint: NSLayoutConstraint!
  29. @IBOutlet weak var scrollView: UIScrollView!
  30. @IBOutlet weak var imageView: UIImageView!
  31. @IBOutlet weak var statusViewImage: UIImageView!
  32. @IBOutlet weak var statusLabel: UILabel!
  33. weak var delegate: NCViewerImagePageContainer?
  34. var image: UIImage?
  35. var metadata: tableMetadata = tableMetadata()
  36. var index: Int = 0
  37. var doubleTapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer()
  38. required init?(coder aDecoder: NSCoder) {
  39. super.init(coder: aDecoder)
  40. self.doubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didDoubleTapWith(gestureRecognizer:)))
  41. self.doubleTapGestureRecognizer.numberOfTapsRequired = 2
  42. }
  43. override func viewDidLoad() {
  44. super.viewDidLoad()
  45. scrollView.delegate = self
  46. scrollView.contentInsetAdjustmentBehavior = .never
  47. if image == nil {
  48. image = CCGraphics.changeThemingColorImage(UIImage.init(named: "noPreview"), width: view.frame.width, height: view.frame.width, color: .gray)
  49. }
  50. if let image = image {
  51. imageView.image = image
  52. imageView.frame = CGRect(x: imageView.frame.origin.x, y: imageView.frame.origin.y, width: image.size.width, height: image.size.height)
  53. }
  54. if NCManageDatabase.sharedInstance.isLivePhoto(metadata: metadata) != nil {
  55. statusViewImage.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "livePhoto"), width: 100, height: 100, color: .gray)
  56. statusLabel.text = NSLocalizedString("_LIVE_", comment: "")
  57. } else if metadata.typeFile == k_metadataTypeFile_video || metadata.typeFile == k_metadataTypeFile_audio {
  58. statusViewImage.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "play"), width: 100, height: 100, color: .gray)
  59. if CCUtility.fileProviderStorageSize(metadata.ocId, fileNameView: metadata.fileNameView) == 0 {
  60. statusLabel.text = NSLocalizedString("_video_streaming_", comment: "")
  61. }
  62. } else {
  63. statusViewImage.image = nil
  64. statusLabel.text = ""
  65. }
  66. view.addGestureRecognizer(doubleTapGestureRecognizer)
  67. }
  68. override func viewWillAppear(_ animated: Bool) {
  69. super.viewWillAppear(animated)
  70. delegate?.viewWillAppearImageZoom(viewerImageZoom: self, metadata: metadata)
  71. }
  72. override func viewDidAppear(_ animated: Bool) {
  73. super.viewDidAppear(animated)
  74. delegate?.viewDidAppearImageZoom(viewerImageZoom: self, metadata: metadata)
  75. }
  76. override func viewDidLayoutSubviews() {
  77. super.viewDidLayoutSubviews()
  78. updateZoomScale()
  79. updateConstraints()
  80. }
  81. //MARK: - Gesture
  82. @objc func didDoubleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  83. let pointInView = gestureRecognizer.location(in: imageView)
  84. var newZoomScale = scrollView.maximumZoomScale
  85. if scrollView.zoomScale >= newZoomScale || abs(scrollView.zoomScale - newZoomScale) <= 0.01 {
  86. newZoomScale = scrollView.minimumZoomScale
  87. }
  88. let width = scrollView.bounds.width / newZoomScale
  89. let height = scrollView.bounds.height / newZoomScale
  90. let originX = pointInView.x - (width / 2.0)
  91. let originY = pointInView.y - (height / 2.0)
  92. let rectToZoomTo = CGRect(x: originX, y: originY, width: width, height: height)
  93. scrollView.zoom(to: rectToZoomTo, animated: true)
  94. }
  95. //MARK: - Function
  96. func updateZoomScale() {
  97. let size = view.bounds.size
  98. let widthScale = size.width / imageView.bounds.width
  99. let heightScale = size.height / imageView.bounds.height
  100. let minScale = min(widthScale, heightScale)
  101. scrollView.minimumZoomScale = minScale
  102. scrollView.zoomScale = minScale
  103. scrollView.maximumZoomScale = minScale * 4
  104. }
  105. func updateConstraints() {
  106. let size = view.bounds.size
  107. let yOffset = max(0, (size.height - imageView.frame.height) / 2)
  108. imageViewTopConstraint.constant = yOffset
  109. imageViewBottomConstraint.constant = yOffset
  110. let xOffset = max(0, (size.width - imageView.frame.width) / 2)
  111. imageViewLeadingConstraint.constant = xOffset
  112. imageViewTrailingConstraint.constant = xOffset
  113. view.layoutIfNeeded()
  114. let contentHeight = yOffset * 2 + imageView.frame.height
  115. scrollView.contentSize = CGSize(width: scrollView.contentSize.width, height: contentHeight)
  116. }
  117. }
  118. extension NCViewerImageZoom: UIScrollViewDelegate {
  119. func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  120. return imageView
  121. }
  122. func scrollViewDidZoom(_ scrollView: UIScrollView) {
  123. updateConstraints()
  124. }
  125. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  126. }
  127. }