MTImagePickerAssetsController.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // ImageSelectorViewController.swift
  3. // CMBMobile
  4. //
  5. // Created by Luo on 5/9/16.
  6. // Copyright © 2016 Yst-WHB. All rights reserved.
  7. //
  8. import UIKit
  9. import AssetsLibrary
  10. import CoreImage
  11. import Foundation
  12. import Photos
  13. class MTImagePickerAssetsController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
  14. weak var delegate: MTImagePickerControllerDelegate?
  15. var maxCount: Int = Int.max
  16. var groupModel: MTImagePickerAlbumModel!
  17. var source: MTImagePickerSource = .ALAsset
  18. @IBOutlet weak var collectionView: MTImagePickerCollectionView!
  19. @IBOutlet weak var lbSelected: UILabel!
  20. @IBOutlet weak var btnPreview: UIButton!
  21. private var dataSource = [MTImagePickerModel]()
  22. private var selectedSource = Set<MTImagePickerModel>()
  23. private var initialScrollDone: Bool = false
  24. private var navigation: MTImagePickerController {
  25. get {
  26. return self.navigationController as! MTImagePickerController
  27. }
  28. }
  29. class var instance: MTImagePickerAssetsController {
  30. get {
  31. let storyboard = UIStoryboard(name: "MTImagePicker", bundle: Bundle.main)
  32. let vc = storyboard.instantiateViewController(withIdentifier: "MTImagePickerController") as! MTImagePickerAssetsController
  33. return vc
  34. }
  35. }
  36. // MARK: Lifecycle
  37. override func viewDidLoad() {
  38. super.viewDidLoad()
  39. let loading = LoadingViewController()
  40. loading.show(text: "Loading...".localized)
  41. self.groupModel?.getMTImagePickerModelsListAsync { (models) in
  42. loading.dismiss()
  43. self.dataSource = models
  44. self.collectionView.reloadData()
  45. self.scrollToBottom()
  46. }
  47. self.initUI()
  48. }
  49. override func viewWillAppear(_ animated: Bool) {
  50. super.viewWillAppear(animated)
  51. self.lbSelected.text = String(self.selectedSource.count)
  52. self.btnPreview.isEnabled = !(self.selectedSource.count == 0)
  53. }
  54. override func viewDidLayoutSubviews() {
  55. super.viewDidLayoutSubviews()
  56. if !self.initialScrollDone {
  57. self.initialScrollDone = true
  58. self.scrollToBottom()
  59. }
  60. }
  61. // MARK: UICollectionViewDelegate
  62. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  63. return self.dataSource.count
  64. }
  65. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  66. let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as! MTImagePickerCell
  67. let model = self.dataSource[indexPath.row]
  68. if model.mediaType == .Video {
  69. cell.videoView.isHidden = false
  70. model.getVideoDurationAsync {
  71. duration in
  72. DispatchQueue.main.async {
  73. cell.videoDuration.text = duration.timeFormat()
  74. }
  75. }
  76. } else {
  77. cell.videoView.isHidden = true
  78. }
  79. cell.imageView.image = model.getThumbImage(size: cell.imageView.frame.size)
  80. cell.indexPath = indexPath
  81. cell.btnCheck.isSelected = self.selectedSource.contains(model)
  82. cell.btnCheck.addTarget(self, action: #selector(MTImagePickerAssetsController.btnCheckTouch(_:)), for: .touchUpInside)
  83. cell.leading.constant = self.collectionView.leading.constant
  84. cell.trailing.constant = self.collectionView.leading.constant
  85. cell.top.constant = self.collectionView.leading.constant * 2
  86. return cell
  87. }
  88. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  89. self.pushToImageSelectorPreviewController(initialIndexPath: indexPath, dataSource: self.dataSource)
  90. }
  91. @objc func btnCheckTouch(_ sender: UIButton) {
  92. if self.selectedSource.count < self.maxCount || sender.isSelected == true {
  93. sender.isSelected = !sender.isSelected
  94. let indexPath = (sender.superview?.superview as! MTImagePickerCell).indexPath
  95. if sender.isSelected {
  96. self.selectedSource.insert(self.dataSource[(indexPath?.row)!])
  97. sender.heartbeatsAnimation(duration: 0.15)
  98. } else {
  99. self.selectedSource.remove(self.dataSource[(indexPath?.row)!])
  100. }
  101. self.lbSelected.text = String(self.selectedSource.count)
  102. self.lbSelected.heartbeatsAnimation(duration: 0.15)
  103. self.btnPreview.isEnabled = !(self.selectedSource.count == 0)
  104. } else {
  105. let alertView = FlashAlertView(message: "Maxium selected".localized, delegate: nil)
  106. alertView.show()
  107. }
  108. }
  109. func showUnAuthorize() {
  110. DispatchQueue.main.async {
  111. let alertView = UIAlertView(title: "Notice".localized, message: "照片访问权限被禁用,请前往系统设置->隐私->照片中,启用本程序对照片的访问权限", delegate: nil, cancelButtonTitle: "OK".localized)
  112. alertView.show()
  113. }
  114. }
  115. //旋转处理
  116. override func willRotate(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval) {
  117. if self.interfaceOrientation.isPortrait != toInterfaceOrientation.isPortrait {
  118. self.collectionView.prevItemSize = (self.collectionView.collectionViewLayout as! MTImagePickerFlowLayout).itemSize
  119. self.collectionView.prevOffset = self.collectionView.contentOffset.y
  120. self.collectionView.collectionViewLayout.invalidateLayout()
  121. }
  122. }
  123. // MARK: private methods
  124. private func scrollToBottom() {
  125. if self.dataSource.count > 0 {
  126. let indexPath = IndexPath(row: self.dataSource.count - 1, section: 0)
  127. self.collectionView.scrollToItem(at: indexPath, at: .bottom, animated: false)
  128. }
  129. }
  130. private func initUI() {
  131. self.title = "All Photos".localized
  132. }
  133. private func pushToImageSelectorPreviewController(initialIndexPath: IndexPath?, dataSource: [MTImagePickerModel]) {
  134. let vc = MTImagePickerPreviewController.instance
  135. vc.dataSource = dataSource
  136. vc.selectedSource = self.selectedSource
  137. vc.initialIndexPath = initialIndexPath
  138. vc.maxCount = self.maxCount
  139. vc.dismiss = {
  140. selectedSource in
  141. self.selectedSource = selectedSource
  142. }
  143. self.navigationController?.pushViewController(vc, animated: true)
  144. }
  145. private func getSelectedSortedSource() -> [MTImagePickerModel] {
  146. var dataSource = [MTImagePickerModel]()
  147. for model in self.selectedSource.sorted(by: { return $0.sortNumber < $1.sortNumber}) {
  148. dataSource.append(model)
  149. }
  150. return dataSource
  151. }
  152. // MARK: IBActions
  153. @IBAction func btnFinishTouch(_ sender: AnyObject) {
  154. let dataSource = self.getSelectedSortedSource()
  155. if self.source == .Photos {
  156. if #available(iOS 8.0, *) {
  157. self.delegate?.imagePickerController?(picker: self.navigation, didFinishPickingWithPhotosModels: dataSource as! [MTImagePickerPhotosModel])
  158. } else {
  159. // Fallback on earlier versions
  160. }
  161. } else {
  162. self.delegate?.imagePickerController?(picker: self.navigation, didFinishPickingWithAssetsModels: dataSource as! [MTImagePickerAssetsModel])
  163. }
  164. self.dismiss(animated: true, completion: nil)
  165. }
  166. @IBAction func btnPreviewTouch(_ sender: AnyObject) {
  167. let dataSource = self.getSelectedSortedSource()
  168. self.pushToImageSelectorPreviewController(initialIndexPath: nil, dataSource: dataSource)
  169. }
  170. @IBAction func btnCancelTouch(_ sender: AnyObject) {
  171. self.delegate?.imagePickerControllerDidCancel?(picker: self.navigation)
  172. self.dismiss(animated: true, completion: nil)
  173. }
  174. }
  175. class MTImagePickerCollectionView: UICollectionView {
  176. @IBOutlet weak var trailing: NSLayoutConstraint!
  177. @IBOutlet weak var leading: NSLayoutConstraint!
  178. var prevItemSize: CGSize?
  179. var prevOffset: CGFloat = 0
  180. }