ZLThumbnailPhotoCell.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //
  2. // ZLThumbnailPhotoCell.swift
  3. // ZLPhotoBrowser
  4. //
  5. // Created by long on 2020/8/12.
  6. //
  7. // Copyright (c) 2020 Long Zhang <495181165@qq.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import UIKit
  27. import Photos
  28. class ZLThumbnailPhotoCell: UICollectionViewCell {
  29. var imageView: UIImageView!
  30. var btnSelect: UIButton!
  31. var bottomShadowView: UIImageView!
  32. var videoTag: UIImageView!
  33. var livePhotoTag: UIImageView!
  34. var editImageTag: UIImageView!
  35. var descLabel: UILabel!
  36. var coverView: UIView!
  37. var indexLabel: UILabel!
  38. var enableSelect: Bool = true
  39. var progressView: ZLProgressView!
  40. var selectedBlock: ( (Bool) -> Void )?
  41. var model: ZLPhotoModel! {
  42. didSet {
  43. self.configureCell()
  44. }
  45. }
  46. var index: Int = 0 {
  47. didSet {
  48. self.indexLabel.text = String(index)
  49. }
  50. }
  51. var imageIdentifier: String = ""
  52. var smallImageRequestID: PHImageRequestID = PHInvalidImageRequestID
  53. var bigImageReqeustID: PHImageRequestID = PHInvalidImageRequestID
  54. deinit {
  55. zl_debugPrint("ZLThumbnailPhotoCell deinit")
  56. }
  57. override init(frame: CGRect) {
  58. super.init(frame: frame)
  59. self.setupUI()
  60. }
  61. required init?(coder: NSCoder) {
  62. fatalError("init(coder:) has not been implemented")
  63. }
  64. func setupUI() {
  65. self.imageView = UIImageView()
  66. self.imageView.contentMode = .scaleAspectFill
  67. self.imageView.clipsToBounds = true
  68. self.contentView.addSubview(self.imageView)
  69. self.coverView = UIView()
  70. self.coverView.isUserInteractionEnabled = false
  71. self.coverView.isHidden = true
  72. self.contentView.addSubview(self.coverView)
  73. self.btnSelect = UIButton(type: .custom)
  74. self.btnSelect.setBackgroundImage(getImage("zl_btn_unselected"), for: .normal)
  75. self.btnSelect.setBackgroundImage(getImage("zl_btn_selected"), for: .selected)
  76. self.btnSelect.addTarget(self, action: #selector(btnSelectClick), for: .touchUpInside)
  77. self.btnSelect.zl_enlargeValidTouchArea(insets: UIEdgeInsets(top: 5, left: 20, bottom: 20, right: 5))
  78. self.contentView.addSubview(self.btnSelect)
  79. self.indexLabel = UILabel()
  80. self.indexLabel.layer.cornerRadius = 23.0 / 2
  81. self.indexLabel.layer.masksToBounds = true
  82. self.indexLabel.textColor = .white
  83. self.indexLabel.font = getFont(14)
  84. self.indexLabel.adjustsFontSizeToFitWidth = true
  85. self.indexLabel.minimumScaleFactor = 0.5
  86. self.indexLabel.textAlignment = .center
  87. self.btnSelect.addSubview(self.indexLabel)
  88. self.bottomShadowView = UIImageView(image: getImage("zl_shadow"))
  89. self.contentView.addSubview(self.bottomShadowView)
  90. self.videoTag = UIImageView(image: getImage("zl_video"))
  91. self.bottomShadowView.addSubview(self.videoTag)
  92. self.livePhotoTag = UIImageView(image: getImage("zl_livePhoto"))
  93. self.bottomShadowView.addSubview(self.livePhotoTag)
  94. self.editImageTag = UIImageView(image: getImage("zl_editImage_tag"))
  95. self.bottomShadowView.addSubview(self.editImageTag)
  96. self.descLabel = UILabel()
  97. self.descLabel.font = getFont(13)
  98. self.descLabel.textAlignment = .right
  99. self.descLabel.textColor = .white
  100. self.bottomShadowView.addSubview(self.descLabel)
  101. self.progressView = ZLProgressView()
  102. self.progressView.isHidden = true
  103. self.contentView.addSubview(self.progressView)
  104. if ZLPhotoConfiguration.default().showSelectedBorder {
  105. self.layer.borderColor = UIColor.selectedBorderColor.cgColor
  106. }
  107. }
  108. override func layoutSubviews() {
  109. self.imageView.frame = self.bounds
  110. self.coverView.frame = self.bounds
  111. self.btnSelect.frame = CGRect(x: self.bounds.width - 30, y: 8, width: 23, height: 23)
  112. self.indexLabel.frame = self.btnSelect.bounds
  113. self.bottomShadowView.frame = CGRect(x: 0, y: self.bounds.height - 25, width: self.bounds.width, height: 25)
  114. self.videoTag.frame = CGRect(x: 5, y: 1, width: 20, height: 15)
  115. self.livePhotoTag.frame = CGRect(x: 5, y: -1, width: 20, height: 20)
  116. self.editImageTag.frame = CGRect(x: 5, y: -1, width: 20, height: 20)
  117. self.descLabel.frame = CGRect(x: 30, y: 1, width: self.bounds.width - 35, height: 17)
  118. self.progressView.frame = CGRect(x: (self.bounds.width - 20)/2, y: (self.bounds.height - 20)/2, width: 20, height: 20)
  119. super.layoutSubviews()
  120. }
  121. @objc func btnSelectClick() {
  122. if !self.enableSelect, ZLPhotoConfiguration.default().showInvalidMask {
  123. return
  124. }
  125. self.btnSelect.layer.removeAllAnimations()
  126. if !self.btnSelect.isSelected {
  127. self.btnSelect.layer.add(getSpringAnimation(), forKey: nil)
  128. }
  129. self.selectedBlock?(self.btnSelect.isSelected)
  130. if self.btnSelect.isSelected {
  131. self.fetchBigImage()
  132. } else {
  133. self.progressView.isHidden = true
  134. self.cancelFetchBigImage()
  135. }
  136. }
  137. func configureCell() {
  138. if ZLPhotoConfiguration.default().cellCornerRadio > 0 {
  139. self.layer.cornerRadius = ZLPhotoConfiguration.default().cellCornerRadio
  140. self.layer.masksToBounds = true
  141. }
  142. if self.model.type == .video {
  143. self.bottomShadowView.isHidden = false
  144. self.videoTag.isHidden = false
  145. self.livePhotoTag.isHidden = true
  146. self.editImageTag.isHidden = true
  147. self.descLabel.text = self.model.duration
  148. } else if self.model.type == .gif {
  149. self.bottomShadowView.isHidden = !ZLPhotoConfiguration.default().allowSelectGif
  150. self.videoTag.isHidden = true
  151. self.livePhotoTag.isHidden = true
  152. self.editImageTag.isHidden = true
  153. self.descLabel.text = "GIF"
  154. } else if self.model.type == .livePhoto {
  155. self.bottomShadowView.isHidden = !ZLPhotoConfiguration.default().allowSelectLivePhoto
  156. self.videoTag.isHidden = true
  157. self.livePhotoTag.isHidden = false
  158. self.editImageTag.isHidden = true
  159. self.descLabel.text = "Live"
  160. } else {
  161. if let _ = self.model.editImage {
  162. self.bottomShadowView.isHidden = false
  163. self.videoTag.isHidden = true
  164. self.livePhotoTag.isHidden = true
  165. self.editImageTag.isHidden = false
  166. self.descLabel.text = ""
  167. } else {
  168. self.bottomShadowView.isHidden = true
  169. }
  170. }
  171. let showSelBtn: Bool
  172. if ZLPhotoConfiguration.default().maxSelectCount > 1 {
  173. if !ZLPhotoConfiguration.default().allowMixSelect {
  174. showSelBtn = self.model.type.rawValue < ZLPhotoModel.MediaType.video.rawValue
  175. } else {
  176. showSelBtn = true
  177. }
  178. } else {
  179. showSelBtn = ZLPhotoConfiguration.default().showSelectBtnWhenSingleSelect
  180. }
  181. self.btnSelect.isHidden = !showSelBtn
  182. self.btnSelect.isUserInteractionEnabled = showSelBtn
  183. self.btnSelect.isSelected = self.model.isSelected
  184. self.indexLabel.backgroundColor = .indexLabelBgColor
  185. if self.model.isSelected {
  186. self.fetchBigImage()
  187. } else {
  188. self.cancelFetchBigImage()
  189. }
  190. if let ei = self.model.editImage {
  191. self.imageView.image = ei
  192. } else {
  193. self.fetchSmallImage()
  194. }
  195. }
  196. func fetchSmallImage() {
  197. let size: CGSize
  198. let maxSideLength = self.bounds.width * 1.2
  199. if self.model.whRatio > 1 {
  200. let w = maxSideLength * self.model.whRatio
  201. size = CGSize(width: w, height: maxSideLength)
  202. } else {
  203. let h = maxSideLength / self.model.whRatio
  204. size = CGSize(width: maxSideLength, height: h)
  205. }
  206. if self.smallImageRequestID > PHInvalidImageRequestID {
  207. PHImageManager.default().cancelImageRequest(self.smallImageRequestID)
  208. }
  209. self.imageIdentifier = self.model.ident
  210. self.imageView.image = nil
  211. self.smallImageRequestID = ZLPhotoManager.fetchImage(for: self.model.asset, size: size, completion: { [weak self] (image, isDegraded) in
  212. if self?.imageIdentifier == self?.model.ident {
  213. self?.imageView.image = image
  214. }
  215. if !isDegraded {
  216. self?.smallImageRequestID = PHInvalidImageRequestID
  217. }
  218. })
  219. }
  220. func fetchBigImage() {
  221. self.cancelFetchBigImage()
  222. self.bigImageReqeustID = ZLPhotoManager.fetchOriginalImageData(for: self.model.asset, progress: { [weak self] (progress, error, _, _) in
  223. if self?.model.isSelected == true {
  224. self?.progressView.isHidden = false
  225. self?.progressView.progress = max(0.1, progress)
  226. self?.imageView.alpha = 0.5
  227. if progress >= 1 {
  228. self?.resetProgressViewStatus()
  229. }
  230. } else {
  231. self?.cancelFetchBigImage()
  232. }
  233. }, completion: { [weak self] (_, _, _) in
  234. self?.resetProgressViewStatus()
  235. })
  236. }
  237. func cancelFetchBigImage() {
  238. if self.bigImageReqeustID > PHInvalidImageRequestID {
  239. PHImageManager.default().cancelImageRequest(self.bigImageReqeustID)
  240. }
  241. self.resetProgressViewStatus()
  242. }
  243. func resetProgressViewStatus() {
  244. self.progressView.isHidden = true
  245. self.imageView.alpha = 1
  246. }
  247. }