CloudFileImageCollectionController.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // CloudFileImageCollectionController.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2019/10/28.
  6. // Copyright © 2019 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. import ImageSlideshow
  11. import QuickLook
  12. private let reuseIdentifier = "CFImageViewCell"
  13. class CloudFileImageCollectionController: UICollectionViewController {
  14. private let horzationGap = 1.toCGFloat
  15. private var fileList: [OOAttachment] = []
  16. private lazy var cFileVM: CloudFileViewModel = {
  17. return CloudFileViewModel()
  18. }()
  19. //预览文件
  20. private lazy var previewVC: CloudFilePreviewController = {
  21. return CloudFilePreviewController()
  22. }()
  23. override func viewDidLoad() {
  24. super.viewDidLoad()
  25. self.collectionView.register(UINib.init(nibName: reuseIdentifier, bundle: nil), forCellWithReuseIdentifier: reuseIdentifier)
  26. self.loadImageList()
  27. }
  28. private func loadImageList() {
  29. self.showLoading()
  30. self.cFileVM.listTypeByPage(type: .image, page: 1, count: 100).then { (result) in
  31. self.fileList = result
  32. self.hideLoading()
  33. self.collectionView.reloadData()
  34. }.catch { (error) in
  35. DDLogError("图片加载失败, \(error.localizedDescription)")
  36. self.hideLoading()
  37. }
  38. }
  39. private func previewFile(fileId: String) {
  40. self.showLoading()
  41. O2CloudFileManager.shared
  42. .getFileUrl(fileId: fileId)
  43. .always {
  44. self.hideLoading()
  45. }
  46. .then { (path) in
  47. let currentURL = NSURL(fileURLWithPath: path.path)
  48. DDLogDebug(currentURL.description)
  49. DDLogDebug(path.path)
  50. if QLPreviewController.canPreview(currentURL) {
  51. self.previewVC.currentFileURLS.removeAll()
  52. self.previewVC.currentFileURLS.append(currentURL)
  53. self.previewVC.reloadData()
  54. self.pushVC(self.previewVC)
  55. }else {
  56. self.showError(title: "当前文件类型不支持预览!")
  57. }
  58. }
  59. .catch { (error) in
  60. DDLogError(error.localizedDescription)
  61. self.showError(title: "获取文件异常!")
  62. }
  63. }
  64. // MARK: UICollectionViewDataSource
  65. override func numberOfSections(in collectionView: UICollectionView) -> Int {
  66. return 1
  67. }
  68. override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  69. return self.fileList.count
  70. }
  71. override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  72. if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as? CFImageViewCell
  73. {
  74. let item = self.fileList[indexPath.row]
  75. let url = self.cFileVM.scaleImageUrl(id: item.id!)
  76. DDLogDebug("url: \(url)")
  77. cell.setData(urlString: url)
  78. return cell
  79. }
  80. return UICollectionViewCell()
  81. }
  82. override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  83. DDLogInfo("点了 row:\(indexPath.row)")
  84. let image = self.fileList[indexPath.row]
  85. self.previewFile(fileId: image.id!)
  86. }
  87. }
  88. extension CloudFileImageCollectionController: UICollectionViewDelegateFlowLayout {
  89. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  90. let width = CGFloat((SCREEN_WIDTH - self.horzationGap * 3.toCGFloat) / 4)
  91. let height = width
  92. return CGSize(width: width, height: height)
  93. }
  94. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  95. return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  96. }
  97. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  98. return 3.0
  99. }
  100. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  101. return 1.0
  102. }
  103. }
  104. extension CloudFileImageCollectionController: CloudFileListBaseChildDelegate {
  105. func reloadDataAndUI() {
  106. self.loadImageList()
  107. }
  108. func reloadUI() {
  109. self.collectionView.reloadData()
  110. }
  111. }