NCViewerMedia.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. //
  2. // NCViewerMedia.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. import SVGKit
  25. import NCCommunication
  26. class NCViewerMedia: UIViewController {
  27. @IBOutlet weak var detailViewTopConstraint: NSLayoutConstraint!
  28. @IBOutlet weak var detailViewHeighConstraint: NSLayoutConstraint!
  29. @IBOutlet weak var imageViewTopConstraint: NSLayoutConstraint!
  30. @IBOutlet weak var imageViewBottomConstraint: NSLayoutConstraint!
  31. @IBOutlet weak var scrollView: UIScrollView!
  32. @IBOutlet weak var imageVideoContainer: imageVideoContainerView!
  33. @IBOutlet weak var statusViewImage: UIImageView!
  34. @IBOutlet weak var statusLabel: UILabel!
  35. @IBOutlet weak var detailView: NCViewerMediaDetailView!
  36. private var _autoPlay: Bool = false
  37. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  38. weak var viewerMediaPage: NCViewerMediaPage?
  39. var playerToolBar: NCPlayerToolBar?
  40. var ncplayer: NCPlayer?
  41. var image: UIImage?
  42. var metadata: tableMetadata = tableMetadata()
  43. var index: Int = 0
  44. var doubleTapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer()
  45. var imageViewConstraint: CGFloat = 0
  46. var isDetailViewInitializze: Bool = false
  47. var autoPlay: Bool {
  48. get {
  49. let temp = _autoPlay
  50. _autoPlay = false
  51. return temp
  52. }
  53. set(newVal) {
  54. _autoPlay = newVal
  55. }
  56. }
  57. // MARK: - View Life Cycle
  58. required init?(coder aDecoder: NSCoder) {
  59. super.init(coder: aDecoder)
  60. doubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didDoubleTapWith(gestureRecognizer:)))
  61. doubleTapGestureRecognizer.numberOfTapsRequired = 2
  62. }
  63. deinit {
  64. print("deinit NCViewerMedia")
  65. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterOpenMediaDetail), object: nil)
  66. }
  67. override func viewDidLoad() {
  68. super.viewDidLoad()
  69. scrollView.delegate = self
  70. scrollView.maximumZoomScale = 4
  71. scrollView.minimumZoomScale = 1
  72. view.addGestureRecognizer(doubleTapGestureRecognizer)
  73. if NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) != nil {
  74. statusViewImage.image = NCUtility.shared.loadImage(named: "livephoto", color: .gray)
  75. statusLabel.text = "LIVE"
  76. } else {
  77. statusViewImage.image = nil
  78. statusLabel.text = ""
  79. }
  80. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  81. playerToolBar = Bundle.main.loadNibNamed("NCPlayerToolBar", owner: self, options: nil)?.first as? NCPlayerToolBar
  82. if let playerToolBar = playerToolBar {
  83. view.addSubview(playerToolBar)
  84. playerToolBar.translatesAutoresizingMaskIntoConstraints = false
  85. playerToolBar.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
  86. playerToolBar.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
  87. playerToolBar.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
  88. playerToolBar.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
  89. playerToolBar.viewerMediaPage = viewerMediaPage
  90. }
  91. let urlVideo = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata)
  92. if let url = urlVideo.url {
  93. self.ncplayer = NCPlayer.init(url: url, autoPlay: self.autoPlay, isProxy: urlVideo.isProxy, imageVideoContainer: self.imageVideoContainer, playerToolBar: self.playerToolBar, metadata: self.metadata, detailView: self.detailView, viewController: self)
  94. }
  95. }
  96. detailViewTopConstraint.constant = 0
  97. detailView.hide()
  98. self.image = nil
  99. self.imageVideoContainer.image = nil
  100. reloadImage()
  101. }
  102. override func viewWillAppear(_ animated: Bool) {
  103. super.viewWillAppear(animated)
  104. viewerMediaPage?.navigationController?.navigationBar.prefersLargeTitles = false
  105. viewerMediaPage?.navigationItem.title = metadata.fileNameView
  106. if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue, let viewerMediaPage = self.viewerMediaPage {
  107. viewerMediaPage.currentScreenMode = viewerMediaPage.saveScreenModeImage
  108. if viewerMediaPage.modifiedOcId.contains(metadata.ocId) {
  109. viewerMediaPage.modifiedOcId.removeAll(where: { $0 == metadata.ocId })
  110. reloadImage()
  111. }
  112. }
  113. if viewerMediaPage?.currentScreenMode == .full {
  114. viewerMediaPage?.navigationController?.setNavigationBarHidden(true, animated: true)
  115. NCUtility.shared.colorNavigationController(viewerMediaPage?.navigationController, backgroundColor: .black, titleColor: .white, tintColor: nil, withoutShadow: false)
  116. viewerMediaPage?.view.backgroundColor = .black
  117. viewerMediaPage?.textColor = .white
  118. viewerMediaPage?.progressView.isHidden = true
  119. } else {
  120. viewerMediaPage?.navigationController?.setNavigationBarHidden(false, animated: true)
  121. NCUtility.shared.colorNavigationController(viewerMediaPage?.navigationController, backgroundColor: NCBrandColor.shared.systemBackground, titleColor: NCBrandColor.shared.label, tintColor: nil, withoutShadow: false)
  122. viewerMediaPage?.view.backgroundColor = NCBrandColor.shared.systemBackground
  123. viewerMediaPage?.textColor = NCBrandColor.shared.label
  124. viewerMediaPage?.progressView.isHidden = false
  125. }
  126. }
  127. override func viewDidAppear(_ animated: Bool) {
  128. super.viewDidAppear(animated)
  129. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  130. if let ncplayer = self.ncplayer {
  131. if !ncplayer.isOpenPlayer {
  132. ncplayer.openAVPlayer()
  133. }
  134. self.viewerMediaPage?.updateCommandCenter(ncplayer: ncplayer, metadata: self.metadata)
  135. }
  136. } else if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  137. viewerMediaPage?.clearCommandCenter()
  138. }
  139. NotificationCenter.default.addObserver(self, selector: #selector(openDetail(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterOpenMediaDetail), object: nil)
  140. }
  141. override func viewDidDisappear(_ animated: Bool) {
  142. super.viewDidDisappear(animated)
  143. }
  144. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  145. super.viewWillTransition(to: size, with: coordinator)
  146. coordinator.animate(alongsideTransition: { context in
  147. // back to the original size
  148. self.scrollView.zoom(to: CGRect(x: 0, y: 0, width: self.scrollView.bounds.width, height: self.scrollView.bounds.height), animated: false)
  149. self.view.layoutIfNeeded()
  150. UIView.animate(withDuration: context.transitionDuration) {
  151. if self.detailView.isShow() {
  152. self.openDetail()
  153. }
  154. }
  155. }) { _ in }
  156. }
  157. // MARK: - Image
  158. func reloadImage() {
  159. if let metadata = NCManageDatabase.shared.getMetadataFromOcId(metadata.ocId) {
  160. self.metadata = metadata
  161. loadImage(metadata: metadata) { _, image in
  162. self.image = image
  163. // do not update if is present the videoLayer
  164. let numSublayers = self.imageVideoContainer.layer.sublayers?.count
  165. if numSublayers == nil {
  166. self.imageVideoContainer.image = image
  167. }
  168. }
  169. }
  170. }
  171. func loadImage(metadata: tableMetadata, completion: @escaping (_ ocId: String, _ image: UIImage?) -> Void) {
  172. // Download preview
  173. if metadata.hasPreview && !CCUtility.fileProviderStoragePreviewIconExists(metadata.ocId, etag: metadata.etag) {
  174. var etagResource: String?
  175. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, account: metadata.account)!
  176. let fileNamePreviewLocalPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)!
  177. let fileNameIconLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)!
  178. if FileManager.default.fileExists(atPath: fileNameIconLocalPath) && FileManager.default.fileExists(atPath: fileNamePreviewLocalPath) {
  179. etagResource = metadata.etagResource
  180. }
  181. NCCommunication.shared.downloadPreview(
  182. fileNamePathOrFileId: fileNamePath,
  183. fileNamePreviewLocalPath: fileNamePreviewLocalPath,
  184. widthPreview: NCGlobal.shared.sizePreview,
  185. heightPreview: NCGlobal.shared.sizePreview,
  186. fileNameIconLocalPath: fileNameIconLocalPath,
  187. sizeIcon: NCGlobal.shared.sizeIcon, etag: etagResource,
  188. queue: NCCommunicationCommon.shared.backgroundQueue) { _, _, imageIcon, _, etag, errorCode, _ in
  189. if errorCode == 0 && imageIcon != nil {
  190. NCManageDatabase.shared.setMetadataEtagResource(ocId: metadata.ocId, etagResource: etag)
  191. }
  192. // Download file max resolution
  193. downloadFile(metadata: metadata)
  194. // Download file live photo
  195. if metadata.livePhoto { downloadFileLivePhoto(metadata: metadata) }
  196. }
  197. } else {
  198. // Download file max resolution
  199. downloadFile(metadata: metadata)
  200. // Download file live photo
  201. if metadata.livePhoto { downloadFileLivePhoto(metadata: metadata) }
  202. }
  203. // Download file max resolution
  204. func downloadFile(metadata: tableMetadata) {
  205. let isFolderEncrypted = CCUtility.isFolderEncrypted(metadata.serverUrl, e2eEncrypted: metadata.e2eEncrypted, account: metadata.account, urlBase: metadata.urlBase)
  206. let ext = CCUtility.getExtension(metadata.fileNameView)
  207. if (CCUtility.getAutomaticDownloadImage() || (metadata.contentType == "image/heic" && metadata.hasPreview == false) || ext == "GIF" || ext == "SVG" || isFolderEncrypted) && (metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue && !CCUtility.fileProviderStorageExists(metadata) && metadata.session == "") {
  208. NCNetworking.shared.download(metadata: metadata, selector: "") { _ in
  209. DispatchQueue.main.async {
  210. let image = getImageMetadata(metadata)
  211. completion(metadata.ocId, image)
  212. }
  213. }
  214. } else {
  215. DispatchQueue.main.async {
  216. let image = getImageMetadata(metadata)
  217. completion(metadata.ocId, image)
  218. }
  219. }
  220. }
  221. // Download Live Photo
  222. func downloadFileLivePhoto(metadata: tableMetadata) {
  223. let fileName = (metadata.fileNameView as NSString).deletingPathExtension + ".mov"
  224. if let metadata = NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameView LIKE[c] %@", metadata.account, metadata.serverUrl, fileName)), !CCUtility.fileProviderStorageExists(metadata) {
  225. NCNetworking.shared.download(metadata: metadata, selector: "") { _ in }
  226. }
  227. }
  228. func getImageMetadata(_ metadata: tableMetadata) -> UIImage? {
  229. if let image = getImage(metadata: metadata) {
  230. return image
  231. }
  232. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue && !metadata.hasPreview {
  233. NCUtility.shared.createImageFrom(fileName: metadata.fileNameView, ocId: metadata.ocId, etag: metadata.etag, classFile: metadata.classFile)
  234. }
  235. if CCUtility.fileProviderStoragePreviewIconExists(metadata.ocId, etag: metadata.etag) {
  236. if let imagePreviewPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag) {
  237. return UIImage(contentsOfFile: imagePreviewPath)
  238. }
  239. }
  240. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  241. return UIImage(named: "noPreviewVideo")!.image(color: .gray, size: view.frame.width)
  242. } else if metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  243. return UIImage(named: "noPreviewAudio")!.image(color: .gray, size: view.frame.width)
  244. } else {
  245. return UIImage(named: "noPreview")!.image(color: .gray, size: view.frame.width)
  246. }
  247. }
  248. func getImage(metadata: tableMetadata) -> UIImage? {
  249. let ext = CCUtility.getExtension(metadata.fileNameView)
  250. var image: UIImage?
  251. if CCUtility.fileProviderStorageExists(metadata) && metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  252. let previewPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)!
  253. let imagePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  254. if ext == "GIF" {
  255. if !FileManager().fileExists(atPath: previewPath) {
  256. NCUtility.shared.createImageFrom(fileName: metadata.fileNameView, ocId: metadata.ocId, etag: metadata.etag, classFile: metadata.classFile)
  257. }
  258. image = UIImage.animatedImage(withAnimatedGIFURL: URL(fileURLWithPath: imagePath))
  259. } else if ext == "SVG" {
  260. if let svgImage = SVGKImage(contentsOfFile: imagePath) {
  261. svgImage.size = CGSize(width: NCGlobal.shared.sizePreview, height: NCGlobal.shared.sizePreview)
  262. if let image = svgImage.uiImage {
  263. if !FileManager().fileExists(atPath: previewPath) {
  264. do {
  265. try image.pngData()?.write(to: URL(fileURLWithPath: previewPath), options: .atomic)
  266. } catch { }
  267. }
  268. return image
  269. } else {
  270. return nil
  271. }
  272. } else {
  273. return nil
  274. }
  275. } else {
  276. NCUtility.shared.createImageFrom(fileName: metadata.fileNameView, ocId: metadata.ocId, etag: metadata.etag, classFile: metadata.classFile)
  277. image = UIImage(contentsOfFile: imagePath)
  278. }
  279. }
  280. return image
  281. }
  282. }
  283. // MARK: - Gesture
  284. @objc func didDoubleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  285. if detailView.isShow() { return }
  286. // NO ZOOM for Audio
  287. if metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue { return }
  288. let pointInView = gestureRecognizer.location(in: self.imageVideoContainer)
  289. var newZoomScale = self.scrollView.maximumZoomScale
  290. if self.scrollView.zoomScale >= newZoomScale || abs(self.scrollView.zoomScale - newZoomScale) <= 0.01 {
  291. newZoomScale = self.scrollView.minimumZoomScale
  292. }
  293. let width = self.scrollView.bounds.width / newZoomScale
  294. let height = self.scrollView.bounds.height / newZoomScale
  295. let originX = pointInView.x - (width / 2.0)
  296. let originY = pointInView.y - (height / 2.0)
  297. let rectToZoomTo = CGRect(x: originX, y: originY, width: width, height: height)
  298. self.scrollView.zoom(to: rectToZoomTo, animated: true)
  299. }
  300. @objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
  301. let currentLocation = gestureRecognizer.translation(in: self.view)
  302. switch gestureRecognizer.state {
  303. case .began:
  304. // let velocity = gestureRecognizer.velocity(in: self.view)
  305. // gesture moving Up
  306. // if velocity.y < 0 {
  307. // }
  308. break
  309. case .ended:
  310. if detailView.isShow() {
  311. self.imageViewTopConstraint.constant = -imageViewConstraint
  312. self.imageViewBottomConstraint.constant = imageViewConstraint
  313. } else {
  314. self.imageViewTopConstraint.constant = 0
  315. self.imageViewBottomConstraint.constant = 0
  316. }
  317. case .changed:
  318. imageViewTopConstraint.constant = (currentLocation.y - imageViewConstraint)
  319. imageViewBottomConstraint.constant = -(currentLocation.y - imageViewConstraint)
  320. // DISMISS VIEW
  321. if detailView.isHidden && (currentLocation.y > 20) {
  322. viewerMediaPage?.navigationController?.popViewController(animated: true)
  323. gestureRecognizer.state = .ended
  324. }
  325. // CLOSE DETAIL
  326. if !detailView.isHidden && (currentLocation.y > 20) {
  327. self.closeDetail()
  328. gestureRecognizer.state = .ended
  329. }
  330. // OPEN DETAIL
  331. if detailView.isHidden && (currentLocation.y < -20) {
  332. self.openDetail()
  333. gestureRecognizer.state = .ended
  334. }
  335. default:
  336. break
  337. }
  338. }
  339. }
  340. // MARK: -
  341. extension NCViewerMedia {
  342. @objc func openDetail(_ notification: NSNotification) {
  343. if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String, ocId == metadata.ocId {
  344. openDetail()
  345. }
  346. }
  347. private func openDetail() {
  348. CCUtility.setExif(metadata) { latitude, longitude, location, date, lensModel in
  349. if latitude != -1 && latitude != 0 && longitude != -1 && longitude != 0 {
  350. self.detailViewHeighConstraint.constant = self.view.bounds.height / 2
  351. } else {
  352. self.detailViewHeighConstraint.constant = 170
  353. }
  354. self.view.layoutIfNeeded()
  355. self.detailView.show(
  356. metadata: self.metadata,
  357. image: self.image,
  358. textColor: self.viewerMediaPage?.textColor,
  359. mediaMetadata: (latitude: latitude, longitude: longitude, location: location, date: date, lensModel: lensModel),
  360. ncplayer: self.ncplayer,
  361. delegate: self)
  362. if let image = self.imageVideoContainer.image {
  363. let ratioW = self.imageVideoContainer.frame.width / image.size.width
  364. let ratioH = self.imageVideoContainer.frame.height / image.size.height
  365. let ratio = ratioW < ratioH ? ratioW : ratioH
  366. let imageHeight = image.size.height * ratio
  367. self.imageViewConstraint = self.detailView.frame.height - ((self.view.frame.height - imageHeight) / 2) + self.view.safeAreaInsets.bottom
  368. if self.imageViewConstraint < 0 { self.imageViewConstraint = 0 }
  369. }
  370. UIView.animate(withDuration: 0.3) {
  371. self.imageViewTopConstraint.constant = -self.imageViewConstraint
  372. self.imageViewBottomConstraint.constant = self.imageViewConstraint
  373. self.detailViewTopConstraint.constant = self.detailViewHeighConstraint.constant
  374. self.view.layoutIfNeeded()
  375. } completion: { _ in
  376. }
  377. self.scrollView.pinchGestureRecognizer?.isEnabled = false
  378. self.playerToolBar?.hide()
  379. }
  380. }
  381. private func closeDetail() {
  382. self.detailView.hide()
  383. imageViewConstraint = 0
  384. UIView.animate(withDuration: 0.3) {
  385. self.imageViewTopConstraint.constant = 0
  386. self.imageViewBottomConstraint.constant = 0
  387. self.detailViewTopConstraint.constant = 0
  388. self.view.layoutIfNeeded()
  389. } completion: { _ in
  390. }
  391. scrollView.pinchGestureRecognizer?.isEnabled = true
  392. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue && !metadata.livePhoto && ncplayer?.player?.timeControlStatus == .paused {
  393. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterShowPlayerToolBar, userInfo: ["ocId": metadata.ocId, "enableTimerAutoHide": false])
  394. }
  395. }
  396. func reloadDetail() {
  397. if self.detailView.isShow() {
  398. CCUtility.setExif(metadata) { (latitude, longitude, location, date, lensModel) in
  399. self.detailView.show(
  400. metadata: self.metadata,
  401. image: self.image,
  402. textColor: self.viewerMediaPage?.textColor,
  403. mediaMetadata: (latitude: latitude, longitude: longitude, location: location, date: date, lensModel: lensModel),
  404. ncplayer: self.ncplayer,
  405. delegate: self)
  406. }
  407. }
  408. }
  409. }
  410. // MARK: -
  411. extension NCViewerMedia: UIScrollViewDelegate {
  412. func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  413. return imageVideoContainer
  414. }
  415. func scrollViewDidZoom(_ scrollView: UIScrollView) {
  416. if scrollView.zoomScale > 1 {
  417. if let image = imageVideoContainer.image {
  418. let ratioW = imageVideoContainer.frame.width / image.size.width
  419. let ratioH = imageVideoContainer.frame.height / image.size.height
  420. let ratio = ratioW < ratioH ? ratioW : ratioH
  421. let newWidth = image.size.width * ratio
  422. let newHeight = image.size.height * ratio
  423. let conditionLeft = newWidth*scrollView.zoomScale > imageVideoContainer.frame.width
  424. let left = 0.5 * (conditionLeft ? newWidth - imageVideoContainer.frame.width : (scrollView.frame.width - scrollView.contentSize.width))
  425. let conditioTop = newHeight*scrollView.zoomScale > imageVideoContainer.frame.height
  426. let top = 0.5 * (conditioTop ? newHeight - imageVideoContainer.frame.height : (scrollView.frame.height - scrollView.contentSize.height))
  427. scrollView.contentInset = UIEdgeInsets(top: top, left: left, bottom: top, right: left)
  428. }
  429. } else {
  430. scrollView.contentInset = .zero
  431. }
  432. }
  433. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  434. }
  435. }
  436. extension NCViewerMedia: NCViewerMediaDetailViewDelegate {
  437. func downloadFullResolution() {
  438. closeDetail()
  439. NCNetworking.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorOpenDetail) { _ in }
  440. }
  441. }
  442. // MARK: -
  443. class imageVideoContainerView: UIImageView {
  444. var playerLayer: CALayer?
  445. var metadata: tableMetadata?
  446. override func layoutSublayers(of layer: CALayer) {
  447. super.layoutSublayers(of: layer)
  448. playerLayer?.frame = self.bounds
  449. }
  450. }