TSAIPhotoBrowseVC.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. //
  2. // TSAIPhotoBrowseVC.swift
  3. // AIEmoji
  4. //
  5. // Created by 100Years on 2025/4/2.
  6. //
  7. private let cellId = "TSAIPhotoBrowseCell"
  8. private let videoCellId = "TSAIVideoBrowseCell"
  9. class TSAIPhotoBrowseVC: TSBaseVC {
  10. var dataModelArray = [TSActionInfoModel]()
  11. var currentImage:UIImage?{
  12. let cell = collectionView.cellForItem(at: IndexPath(item: currentIndex, section: 0)) as? TSAIPhotoBrowseCell
  13. var image = cell?.netWorkImageView.image
  14. image = image?.pngImage
  15. return image
  16. }
  17. var currentModel:TSActionInfoModel?{
  18. if let model = dataModelArray.safeObj(At: currentIndex){
  19. return model
  20. }
  21. return nil
  22. }
  23. var currentIndex:Int = 0 {
  24. didSet{
  25. reloadUI()
  26. }
  27. }
  28. lazy var collectionView: UICollectionView = {
  29. let layout = UICollectionViewFlowLayout()
  30. layout.scrollDirection = .vertical
  31. let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
  32. collectionView.delegate = self
  33. collectionView.dataSource = self
  34. collectionView.showsVerticalScrollIndicator = false
  35. collectionView.showsHorizontalScrollIndicator = false
  36. collectionView.backgroundColor = .clear
  37. if #available(iOS 11.0, *) {
  38. collectionView.contentInsetAdjustmentBehavior = .never
  39. }
  40. collectionView.register(TSAIPhotoBrowseCell.self, forCellWithReuseIdentifier: cellId)
  41. collectionView.register(TSAIVideoBrowseCell.self, forCellWithReuseIdentifier: videoCellId)
  42. collectionView.isPagingEnabled = true
  43. collectionView.isHidden = true
  44. if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
  45. flowLayout.minimumInteritemSpacing = 0
  46. flowLayout.minimumLineSpacing = 0
  47. flowLayout.scrollDirection = .horizontal
  48. flowLayout.itemSize = CGSize(width: k_ScreenWidth, height: k_ScreenHeight)
  49. }
  50. return collectionView
  51. }()
  52. //保存按钮
  53. lazy var bigSaveBtn: UIButton = {
  54. let bigSaveBtn = kCreateNormalSubmitBtn(title: "Save".localized) { [weak self] in
  55. guard let self = self else { return }
  56. clickSubmitBtn()
  57. }
  58. bigSaveBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  59. bigSaveBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  60. bigSaveBtn.frame = CGRectMake(0, 0, 252, 44)
  61. bigSaveBtn.cornerRadius = 22.0
  62. return bigSaveBtn
  63. }()
  64. lazy var xBtn: UIButton = {
  65. let xBtn = UIButton.createButton(image: UIImage(named: "close_gray")) { [weak self] in
  66. guard let self = self else { return }
  67. clickXBtn()
  68. }
  69. return xBtn
  70. }()
  71. override func createView() {
  72. super.createView()
  73. setNavBarViewHidden(true)
  74. contentView.addSubview(collectionView)
  75. collectionView.snp.makeConstraints { make in
  76. make.edges.equalTo(0)
  77. }
  78. DispatchQueue.main.async {
  79. self.collectionView.isHidden = false
  80. self.collectionView.reloadData()
  81. self.collectionView.setContentOffset(CGPoint(x: CGFloat(self.currentIndex) * self.collectionView.frame.size.width, y: 0), animated: false)
  82. if let flowLayout = self.collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
  83. flowLayout.itemSize = self.collectionView.bounds.size
  84. }
  85. }
  86. contentView.addSubview(bigSaveBtn)
  87. bigSaveBtn.snp.makeConstraints { make in
  88. make.centerX.equalToSuperview()
  89. make.bottom.equalTo(-8-k_Height_safeAreaInsetsBottom())
  90. make.width.equalTo(bigSaveBtn.width)
  91. make.height.equalTo(bigSaveBtn.height)
  92. }
  93. //关闭按钮
  94. contentView.addSubview(xBtn)
  95. xBtn.snp.makeConstraints { make in
  96. make.top.equalTo(k_Height_StatusBar + 4)
  97. make.leading.equalTo(16)
  98. make.width.equalTo(36)
  99. make.height.equalTo(36)
  100. }
  101. }
  102. override func dealThings() {
  103. // 添加下拉手势
  104. // let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handleDismissPan(_:)))
  105. // view.addGestureRecognizer(panGesture)
  106. addPullDownClosePage()
  107. }
  108. @objc func clickSubmitBtn(){
  109. if JudgeVip(){
  110. return
  111. }
  112. guard let currentModel = currentModel else { return }
  113. let urlString = currentModel.response.resultUrl
  114. if currentModel.isVideo {
  115. TSDownloadManager.getDownLoadVideo(urlString: urlString) { url, _ in
  116. if let url = url {
  117. PhotoManagerShared.saveVideoToAlbum(videoURL: url) { success, error in
  118. if success {
  119. kSaveSuccesswShared.show(atView: self.view)
  120. }else{
  121. debugPrint(error)
  122. }
  123. }
  124. }
  125. }
  126. }else{
  127. UIImageView.downloadImageWithProgress(urlString: urlString) { image in
  128. if let image = image{
  129. PhotoManagerShared.saveImageToAlbum(image) { success, error in
  130. if success {
  131. kSaveSuccesswShared.show(atView: self.view)
  132. }else{
  133. debugPrint(error)
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }
  140. func JudgeVip() -> Bool {
  141. return kJudgeVip(externalBool: currentModel?.response.vip ?? false , vc: self, closePageBlock: nil)
  142. }
  143. @objc func clickXBtn(){
  144. pop()
  145. }
  146. }
  147. extension TSAIPhotoBrowseVC {
  148. // 手势处理方法
  149. // @objc func handleDismissPan(_ gesture: UIPanGestureRecognizer) {
  150. // let translation = gesture.translation(in: view)
  151. // let progress = translation.y / view.bounds.height
  152. //
  153. // switch gesture.state {
  154. // case .changed:
  155. // view.transform = CGAffineTransform(translationX: 0, y: max(0, translation.y))
  156. // case .ended:
  157. // if progress > 0.5 || gesture.velocity(in: view).y > 1000 {
  158. // dismiss(animated: true)
  159. // } else {
  160. // UIView.animate(withDuration: 0.3) {
  161. // self.view.transform = .identity
  162. // }
  163. // }
  164. // default:
  165. // break
  166. // }
  167. // }
  168. func reloadUI() {
  169. }
  170. }
  171. //MARK: UICollectionViewDataSource
  172. extension TSAIPhotoBrowseVC:UICollectionViewDataSource,UICollectionViewDelegate {
  173. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  174. resetIndexWithOffset(scrollView)
  175. }
  176. func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
  177. resetIndexWithOffset(scrollView)
  178. }
  179. private func resetIndexWithOffset(_ scrollView: UIScrollView) {
  180. let item = Int((scrollView.contentOffset.x / scrollView.bounds.width).rounded())
  181. currentIndex = item
  182. }
  183. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  184. return dataModelArray.count
  185. }
  186. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  187. if let model = dataModelArray.safeObj(At: indexPath.item){
  188. if model.isVideo {
  189. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: videoCellId, for: indexPath) as! TSAIVideoBrowseCell
  190. cell.model = model
  191. return cell
  192. }
  193. }
  194. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! TSAIPhotoBrowseCell
  195. if let model = dataModelArray.safeObj(At: indexPath.item){
  196. cell.model = model
  197. }
  198. return cell
  199. }
  200. func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  201. if let model = dataModelArray.safeObj(At: indexPath.item){
  202. if model.isVideo {
  203. guard let cell = cell as? TSAIVideoBrowseCell else { return }
  204. cell.model = model
  205. }else{
  206. guard let cell = cell as? TSAIPhotoBrowseCell else { return }
  207. cell.model = model
  208. }
  209. }
  210. }
  211. func collectionView(_ collectionView: UICollectionView,
  212. didEndDisplaying cell: UICollectionViewCell,
  213. forItemAt indexPath: IndexPath) {
  214. if let model = dataModelArray.safeObj(At: indexPath.item){
  215. if model.isVideo {
  216. guard let cell = cell as? TSAIVideoBrowseCell else { return }
  217. cell.videoPlayerVC?.playPause()
  218. }
  219. }
  220. }
  221. }
  222. class TSAIPhotoBrowseCell : TSBaseCollectionCell{
  223. lazy var exampleView: UIView = {
  224. let exampleView = UIView()
  225. exampleView.backgroundColor = "#232323".uiColor.withAlphaComponent(0.3)
  226. let textLabel = UILabel.createLabel(
  227. text: "Example".localized,
  228. font: .font(size: 12),
  229. textColor: .white
  230. )
  231. exampleView.addSubview(textLabel)
  232. textLabel.snp.makeConstraints { make in
  233. make.top.edges.equalTo(UIEdgeInsets(top: 4, left: 6, bottom: 4, right: 6))
  234. }
  235. exampleView.isHidden = true
  236. exampleView.cornerRadius = 10.0
  237. return exampleView
  238. }()
  239. lazy var netWorkImageView : UIImageView = {
  240. let netWorkImageView = UIImageView.createImageView(imageName: "",contentMode: .scaleAspectFit,corner: 24.0)
  241. return netWorkImageView
  242. }()
  243. lazy var vipImageView: UIImageView = {
  244. let vipImageView = UIImageView.createImageView(imageName:"vip_side_icon")
  245. vipImageView.contentMode = .scaleToFill
  246. vipImageView.isHidden = true
  247. return vipImageView
  248. }()
  249. override func creatUI() {
  250. let w = k_ScreenWidth
  251. let h = k_ScreenHeight
  252. bgContentView.addSubview(netWorkImageView)
  253. netWorkImageView.snp.makeConstraints { make in
  254. make.center.equalToSuperview()
  255. make.width.equalTo(w)
  256. make.height.equalTo(h)
  257. }
  258. netWorkImageView.addSubview(vipImageView)
  259. vipImageView.snp.makeConstraints { make in
  260. make.width.height.equalTo(40)
  261. make.top.equalTo(-5)
  262. make.trailing.equalTo(5)
  263. }
  264. }
  265. var model:TSActionInfoModel = TSActionInfoModel(){
  266. didSet{
  267. dePrint("TSAIPhotoBrowseCell didSet model")
  268. if model.modelType == .example {
  269. netWorkImageView.image = UIImage(named:model.response.resultUrl)
  270. netWorkImageView.adaptiveScale()
  271. }else{
  272. netWorkImageView.setAsyncImage(urlString: model.response.resultUrl,placeholder: kPlaceholderImage,contentMode: .scaleAspectFit, completion: { [weak self] image in
  273. guard let self = self else { return }
  274. netWorkImageView.adaptiveScale()
  275. })
  276. }
  277. vipImageView.isHidden = !model.response.vip
  278. }
  279. }
  280. }
  281. class TSAIVideoBrowseCell : TSBaseCollectionCell{
  282. var videoPlayerVC: TSAIListVideoPlayerVC?
  283. override func creatUI() {
  284. }
  285. var model:TSActionInfoModel = TSActionInfoModel(){
  286. didSet{
  287. self.videoPlayerVC?.view.removeFromSuperview()
  288. self.videoPlayerVC = TSAIListVideoPlayerVC(videoURL: self.model.videoURL)
  289. self.bgContentView.addSubview(self.videoPlayerVC!.view)
  290. self.videoPlayerVC!.view.snp.remakeConstraints { make in
  291. make.center.equalToSuperview()
  292. make.width.equalTo(k_ScreenWidth)
  293. make.height.equalTo(k_ScreenHeight)
  294. }
  295. }
  296. }
  297. }