PQUploadController.swift 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. //
  2. // PQUploadViewController.swift
  3. // PQSpeed
  4. //
  5. // Created by SanW on 2020/8/1.
  6. // Copyright © 2020 BytesFlow. All rights reserved.
  7. //
  8. import MobileCoreServices
  9. import Photos
  10. import UIKit
  11. let playerHeaderH: CGFloat = cScreenWidth * (250 / 375)
  12. class PQUploadController: PQBaseViewController {
  13. // 最大的宽度
  14. var maxWidth: CGFloat = cScreenWidth
  15. // 最大的高度
  16. var maxHeight: CGFloat = adapterWidth(width: 300)
  17. // 画面比例
  18. var aspectRatio: aspectRatio?
  19. var preViewSize: CGSize {
  20. if aspectRatio == nil, (isMember(of: PQUploadController.self) && selectedData == nil) || (!isMember(of: PQUploadController.self) && uploadData == nil) {
  21. return CGSize(width: maxHeight, height: maxHeight)
  22. }
  23. if selectedData != nil {
  24. aspectRatio = .origin(width: CGFloat(videoData[lastSeletedIndex?.item ?? 0].videoWidth), height: CGFloat(videoData[lastSeletedIndex?.item ?? 0].videoHeight))
  25. } else if aspectRatio == nil {
  26. aspectRatio = .origin(width: CGFloat(uploadData?.videoWidth ?? maxHeight), height: CGFloat(uploadData?.videoHeight ?? maxHeight))
  27. }
  28. switch aspectRatio {
  29. case let .origin(width, height):
  30. var tempHeight: CGFloat = 0
  31. var tempWidth: CGFloat = 0
  32. if width > height {
  33. tempHeight = (maxWidth * height / width)
  34. tempWidth = maxWidth
  35. } else {
  36. tempHeight = maxHeight
  37. tempWidth = (maxHeight * width / height)
  38. }
  39. if tempHeight.isNaN || tempWidth.isNaN {
  40. return CGSize.zero
  41. } else {
  42. return CGSize(width: tempWidth, height: tempHeight)
  43. }
  44. case .oneToOne:
  45. if maxWidth > maxHeight {
  46. return CGSize(width: maxHeight, height: maxHeight)
  47. } else {
  48. return CGSize(width: maxWidth, height: maxWidth)
  49. }
  50. case .sixteenToNine:
  51. return CGSize(width: maxWidth, height: maxWidth * 9.0 / 16.0)
  52. case .nineToSixteen:
  53. return CGSize(width: maxHeight * 9.0 / 16.0, height: maxHeight)
  54. default:
  55. break
  56. }
  57. return CGSize(width: maxHeight, height: maxHeight)
  58. }
  59. // 上传入口类型
  60. var sourceType: videoUploadSourceType = .videoUpload
  61. // 制作视频项目Id
  62. var makeVideoProjectId: String?
  63. // 再创作数据
  64. var reCreateData: PQReCreateModel?
  65. // 视频创作埋点数据
  66. var eventTrackData: PQVideoMakeEventTrackModel?
  67. // 制作视频草稿Id
  68. var makeVideoDraftboxId: String?
  69. // 制作视频草稿结构化数据
  70. var makeVideoSdata: String?
  71. var isAssetImage: Bool = false // 是否请求的是图片
  72. var videoWidth: CGFloat = 0 // 视频宽
  73. var videoHeight: CGFloat = 0 // 视频高
  74. var isLoop: Bool = true // 是否循环播放
  75. var playerItem: AVPlayerItem? // 当前播放item
  76. var videoOutput: AVPlayerItemVideoOutput?
  77. let itemSize = CGSize(width: ((cScreenWidth - cDefaultMargin) / 3) * UIScreen.main.scale, height: ((cScreenWidth - cDefaultMargin) / 3) * UIScreen.main.scale)
  78. var categoryH: CGFloat = cDefaultMargin * 40 // 相簿高度
  79. var videoData: [PQUploadModel] = Array<PQUploadModel>.init()
  80. var categoryData: [PQUploadModel] = Array<PQUploadModel>.init()
  81. // 当前选中的数据
  82. var selectedData: PQUploadModel?
  83. // 确定上传的数据
  84. var uploadData: PQUploadModel?
  85. var catagerySelectedIndex: IndexPath = IndexPath(item: 0, section: 0) // 更多图库选择
  86. var isJumpToAuthorization: Bool = false // 是否跳到授权
  87. var lastSeletedIndex: IndexPath? // 上次选中的index
  88. lazy var imageManager: PHCachingImageManager = {
  89. (PHCachingImageManager.default() as? PHCachingImageManager) ?? PHCachingImageManager()
  90. }()
  91. var allPhotos: PHFetchResult<PHAsset>!
  92. var previousPreheatRect = CGRect.zero
  93. var smartAlbums: PHFetchResult<PHAssetCollection>!
  94. var userCollections: PHFetchResult<PHCollection>!
  95. let sectionLocalizedTitles = ["", NSLocalizedString("Smart Albums", comment: ""), NSLocalizedString("Albums", comment: "")]
  96. lazy var fetchOptions: PHFetchOptions = {
  97. let fetchOptions = PHFetchOptions()
  98. fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
  99. fetchOptions.predicate = NSPredicate(format: "mediaType = %d", !isAssetImage ? PHAssetMediaType.video.rawValue : PHAssetMediaType.image.rawValue)
  100. return fetchOptions
  101. }()
  102. lazy var imagesOptions: PHImageRequestOptions = {
  103. let imagesOptions = PHImageRequestOptions()
  104. imagesOptions.isSynchronous = true
  105. imagesOptions.isNetworkAccessAllowed = false
  106. imagesOptions.deliveryMode = .opportunistic
  107. imagesOptions.resizeMode = .fast
  108. imagesOptions.progressHandler = { _, _, _, info in
  109. BFLog(message: "progressHandler = \(info)")
  110. }
  111. return imagesOptions
  112. }()
  113. lazy var singleImageOptions: PHImageRequestOptions = {
  114. let singleImageOptions = PHImageRequestOptions()
  115. singleImageOptions.isSynchronous = true
  116. singleImageOptions.isNetworkAccessAllowed = false
  117. singleImageOptions.deliveryMode = .highQualityFormat
  118. return singleImageOptions
  119. }()
  120. lazy var backBtn: UIButton = {
  121. let backBtn = UIButton(type: .custom)
  122. backBtn.frame = CGRect(x: 0, y: cDevice_iPhoneStatusBarHei, width: cDefaultMargin * 4, height: cDefaultMargin * 4)
  123. backBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: -5, right: 0)
  124. backBtn.backgroundColor = .yellow
  125. backBtn.setImage(UIImage().BF_Image(named: "icon_blanc_back"), for: .normal)
  126. backBtn.addTarget(self, action: #selector(backBtnClick), for: .touchUpInside)
  127. return backBtn
  128. }()
  129. var anthorEmptyData: PQEmptyModel? = {
  130. let anthorEmptyData = PQEmptyModel()
  131. anthorEmptyData.title = "开始上传"
  132. anthorEmptyData.summary = "要开始上传视频,请先授予相册使用权限"
  133. anthorEmptyData.emptyImage = "icon_authorError"
  134. return anthorEmptyData
  135. }()
  136. var emptyData: PQEmptyModel? = {
  137. let emptyData = PQEmptyModel()
  138. emptyData.title = "哦呜~ 你没有可上传的视频~"
  139. emptyData.emptyImage = "video_empty"
  140. return emptyData
  141. }()
  142. lazy var emptyRemindView: PQEmptyRemindView = {
  143. let remindView = PQEmptyRemindView(frame: CGRect(x: 0, y: cDevice_iPhoneNavBarAndStatusBarHei, width: cScreenWidth, height: cScreenHeigth - cDevice_iPhoneNavBarAndStatusBarHei))
  144. remindView.isHidden = true
  145. remindView.emptyData = anthorEmptyData
  146. view.addSubview(remindView)
  147. remindView.fullRefreshBloc = { [weak self] _, _ in
  148. self?.isJumpToAuthorization = true
  149. if self?.emptyRemindView.refreshBtn.currentTitle == "授予权限" {
  150. openAppSetting()
  151. }
  152. }
  153. return remindView
  154. }()
  155. lazy var collectionView: UICollectionView = {
  156. let layout = UICollectionViewFlowLayout()
  157. layout.sectionInset = UIEdgeInsets.zero
  158. let collectionView = UICollectionView(frame: CGRect(x: 0, y: cDevice_iPhoneNavBarAndStatusBarHei, width: cScreenWidth, height: cScreenHeigth - cDevice_iPhoneNavBarAndStatusBarHei), collectionViewLayout: layout)
  159. collectionView.showsVerticalScrollIndicator = false
  160. collectionView.register(PQSelecteVideoItemCell.self, forCellWithReuseIdentifier: "PQSelecteVideoItemCell")
  161. collectionView.delegate = self
  162. collectionView.dataSource = self
  163. if #available(iOS 11.0, *) {
  164. collectionView.contentInsetAdjustmentBehavior = .never
  165. } else {
  166. automaticallyAdjustsScrollViewInsets = false
  167. }
  168. collectionView.backgroundColor = .clear
  169. return collectionView
  170. }()
  171. lazy var categoryCollectionView: UICollectionView = {
  172. let layout = UICollectionViewFlowLayout()
  173. layout.sectionInset = UIEdgeInsets.zero
  174. let categoryCollectionView = UICollectionView(frame: CGRect(x: 0, y: -categoryH, width: cScreenWidth, height: categoryH), collectionViewLayout: layout)
  175. categoryCollectionView.showsVerticalScrollIndicator = false
  176. categoryCollectionView.register(PQAssetCategoryCell.self, forCellWithReuseIdentifier: "PQAssetCategoryCell")
  177. categoryCollectionView.delegate = self
  178. categoryCollectionView.dataSource = self
  179. if #available(iOS 11.0, *) {
  180. categoryCollectionView.contentInsetAdjustmentBehavior = .never
  181. } else {
  182. automaticallyAdjustsScrollViewInsets = false
  183. }
  184. categoryCollectionView.backgroundColor = .white
  185. return categoryCollectionView
  186. }()
  187. lazy var categoryView: UIView = {
  188. let categoryView = UIView(frame: CGRect(x: 0, y: cDevice_iPhoneNavBarAndStatusBarHei, width: cScreenWidth, height: cScreenHeigth - cDevice_iPhoneNavBarAndStatusBarHei))
  189. categoryView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.6)
  190. let bgView = UIView(frame: CGRect(x: 0, y: categoryH, width: categoryView.frame.width, height: categoryView.frame.height - categoryH))
  191. let ges = UITapGestureRecognizer(target: self, action: #selector(cancelClick))
  192. bgView.addGestureRecognizer(ges)
  193. categoryView.addSubview(bgView)
  194. categoryView.isHidden = true
  195. return categoryView
  196. }()
  197. lazy var playBtn: UIButton = {
  198. let playBtn = UIButton(type: .custom)
  199. playBtn.frame = CGRect(x: (preViewSize.width - cDefaultMargin * 5) / 2, y: (preViewSize.height - cDefaultMargin * 5) / 2, width: cDefaultMargin * 5, height: cDefaultMargin * 5)
  200. playBtn.setImage(UIImage(named: "icon_video_play_big"), for: .normal)
  201. playBtn.tag = 4
  202. playBtn.isHidden = true
  203. playBtn.isUserInteractionEnabled = false
  204. // playBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
  205. return playBtn
  206. }()
  207. var playerHeaderView: UIImageView = {
  208. let playerHeaderView = UIImageView(frame: CGRect(x: 0, y: cDevice_iPhoneNavBarAndStatusBarHei, width: cScreenWidth, height: 0))
  209. playerHeaderView.isUserInteractionEnabled = true
  210. playerHeaderView.contentMode = .scaleAspectFit
  211. playerHeaderView.clipsToBounds = true
  212. playerHeaderView.backgroundColor = UIColor.yellow
  213. return playerHeaderView
  214. }()
  215. lazy var selecteBtn: UIButton = {
  216. let selecteBtn = UIButton(frame: CGRect(x: deleteBtn.frame.maxX + cDefaultMargin, y: 0, width: cScreenWidth - nextBtn.frame.width - deleteBtn.frame.maxX - cDefaultMargin * 5, height: cDevice_iPhoneTabBarHei))
  217. selecteBtn.titleLabel?.lineBreakMode = .byTruncatingTail
  218. selecteBtn.setTitle("全部", for: .normal)
  219. selecteBtn.setImage(UIImage().BF_Image(named: "icon_uploadVideo_more"), for: .normal)
  220. selecteBtn.setTitleColor(UIColor.black, for: .normal)
  221. selecteBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .medium)
  222. selecteBtn.tag = 2
  223. selecteBtn.imagePosition(at: .right, space: cDefaultMargin / 2)
  224. selecteBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
  225. return selecteBtn
  226. }()
  227. lazy var deleteBtn: UIButton = {
  228. let deleteBtn = UIButton(frame: CGRect(x: cDefaultMargin, y: 0, width: cDefaultMargin * 4, height: cDevice_iPhoneTabBarHei))
  229. deleteBtn.setImage( UIImage().BF_Image(named: "icon_blanc_back"), for: .normal)
  230. deleteBtn.tag = 1
  231. deleteBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
  232. return deleteBtn
  233. }()
  234. lazy var nextBtn: UIButton = {
  235. let nextBtn = UIButton(frame: CGRect(x: 0, y: (cDevice_iPhoneTabBarHei - cDefaultMargin * 3) / 2, width: cDefaultMargin * 6, height: cDefaultMargin * 3))
  236. nextBtn.addCorner(corner: 3)
  237. nextBtn.setTitle("下一步", for: .normal)
  238. nextBtn.titleLabel?.font = UIFont.systemFont(ofSize: 13, weight: .medium)
  239. nextBtn.tag = 3
  240. nextBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
  241. nextBtn.backgroundColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
  242. nextBtn.setTitleColor(.white, for: .normal)
  243. return nextBtn
  244. }()
  245. lazy var bottomView: UIView = {
  246. let bottomView = UIView(frame: CGRect(x: 0, y: cDefaultMargin * 2, width: cScreenWidth, height: cDevice_iPhoneNavBarHei))
  247. bottomView.addSubview(selecteBtn)
  248. bottomView.backgroundColor = PQBFConfig.shared.styleBackGroundColor
  249. selecteBtn.center.y = nextBtn.center.y
  250. bottomView.addSubview(deleteBtn)
  251. bottomView.addSubview(nextBtn)
  252. nextBtn.frame.origin.x = bottomView.frame.width - nextBtn.frame.width - cDefaultMargin * 2
  253. return bottomView
  254. }()
  255. override func viewDidLoad() {
  256. super.viewDidLoad()
  257. view.backgroundColor = .white
  258. navHeadImageView?.backgroundColor = .white
  259. lineView?.backgroundColor = .white
  260. addSubViews()
  261. loadLocalData()
  262. }
  263. deinit {
  264. // PHPhotoLibrary.shared().unregisterChangeObserver(self)
  265. }
  266. override func viewDidDisappear(_ animated: Bool) {
  267. super.viewDidDisappear(animated)
  268. }
  269. override func viewWillAppear(_ animated: Bool) {
  270. super.viewDidAppear(animated)
  271. addPlayerItemObserver()
  272. }
  273. override func viewWillDisappear(_ animated: Bool) {
  274. super.viewWillDisappear(animated)
  275. removePlayerItemObserver()
  276. }
  277. func addSubViews() {
  278. view.addSubview(collectionView)
  279. navHeadImageView?.addSubview(bottomView)
  280. PQNotification.addObserver(self, selector: #selector(didBecomeActiveNotification), name: UIApplication.didBecomeActiveNotification, object: nil)
  281. }
  282. func loadLocalData() {
  283. let authStatus = PHPhotoLibrary.authorizationStatus()
  284. if authStatus == .notDetermined {
  285. // 第一次触发授权 alert
  286. PHPhotoLibrary.requestAuthorization { [weak self] (status: PHAuthorizationStatus) -> Void in
  287. if status == .authorized {
  288. if (self?.allPhotos == nil) || (self?.allPhotos.count ?? 0) <= 0 {
  289. self?.loadPhotoData()
  290. }
  291. if self?.backBtn != nil {
  292. self?.backBtn.removeFromSuperview()
  293. }
  294. } else {
  295. DispatchQueue.main.async { [weak self] in
  296. self?.emptyRemindView.isHidden = false
  297. self?.emptyRemindView.refreshBtn.isHidden = false
  298. self?.emptyRemindView.refreshBtn.setTitle("授予权限", for: .normal)
  299. if self?.backBtn.superview == nil {
  300. self?.view.addSubview((self?.backBtn)!)
  301. } else {
  302. self?.backBtn.isHidden = false
  303. }
  304. }
  305. }
  306. }
  307. } else if authStatus == .authorized {
  308. if allPhotos == nil || allPhotos.count <= 0 {
  309. loadPhotoData()
  310. }
  311. if backBtn.superview != nil {
  312. backBtn.removeFromSuperview()
  313. }
  314. } else {
  315. emptyRemindView.isHidden = false
  316. emptyRemindView.refreshBtn.isHidden = false
  317. emptyRemindView.refreshBtn.setTitle("授予权限", for: .normal)
  318. if backBtn.superview == nil {
  319. view.addSubview(backBtn)
  320. } else {
  321. backBtn.isHidden = false
  322. }
  323. }
  324. }
  325. func loadPhotoData() {
  326. DispatchQueue.main.async { [weak self] in
  327. PQLoadingHUB.shared.showHUB(superView: self!.view, isVerticality: false)
  328. }
  329. DispatchQueue.global().async { [weak self] in
  330. self?.allPhotos = PHAsset.fetchAssets(with: self?.fetchOptions)
  331. DispatchQueue.main.async { [weak self] in
  332. if self?.view != nil {
  333. PQLoadingHUB.shared.dismissHUB(superView: self!.view)
  334. }
  335. self?.collectionView.reloadData()
  336. if (self?.allPhotos.count ?? 0) <= 0 {
  337. self?.emptyRemindView.frame = CGRect(x: 0, y: cDevice_iPhoneNavBarAndStatusBarHei, width: cScreenWidth, height: cScreenHeigth - cDevice_iPhoneNavBarAndStatusBarHei - cDevice_iPhoneTabBarHei)
  338. self?.emptyRemindView.emptyData = self?.emptyData
  339. self?.emptyRemindView.isHidden = false
  340. } else {
  341. self?.emptyRemindView.isHidden = true
  342. }
  343. if (self?.allPhotos.count ?? 0) > 0 {
  344. let tempData = PQUploadModel()
  345. tempData.title = "全部"
  346. tempData.categoryList = self!.allPhotos
  347. self?.categoryData.insert(tempData, at: 0)
  348. }
  349. self?.updateCachedAssets()
  350. }
  351. }
  352. PHPhotoLibrary.shared().register(self)
  353. DispatchQueue.global().async { [weak self] in
  354. self?.smartAlbums = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .albumRegular, options: nil)
  355. self?.smartAlbums?.enumerateObjects { [weak self] assCollection, _, _ in
  356. if assCollection.localizedTitle != "最近删除" {
  357. self?.convertCollection(collection: assCollection)
  358. }
  359. }
  360. self?.userCollections = PHCollectionList.fetchTopLevelUserCollections(with: nil)
  361. self?.userCollections.enumerateObjects { assCollection, index, point in
  362. BFLog(message: "userCollections == \(assCollection),index = \(index),point = \(point)")
  363. if assCollection is PHAssetCollection {
  364. if assCollection.localizedTitle != "最近删除" {
  365. self?.convertCollection(collection: assCollection as? PHAssetCollection)
  366. }
  367. }
  368. }
  369. }
  370. }
  371. // 转化处理获取到的相簿
  372. func convertCollection(collection: PHAssetCollection?) {
  373. if collection == nil {
  374. return
  375. }
  376. DispatchQueue.global().async { [weak self] in
  377. let assetsFetchResult = PHAsset.fetchAssets(in: collection!, options: self?.fetchOptions)
  378. if assetsFetchResult.count > 0 {
  379. let tempData = PQUploadModel()
  380. tempData.title = collection?.localizedTitle
  381. tempData.categoryList = assetsFetchResult
  382. if tempData.categoryList.count > 0 {
  383. self?.categoryData.append(tempData)
  384. }
  385. BFLog(message: "assetsFetchResult = \(assetsFetchResult)")
  386. }
  387. }
  388. }
  389. @objc func btnClick(sender: UIButton) {
  390. switch sender.tag {
  391. case 1: // 返回
  392. navigationController?.popViewController(animated: true)
  393. case 2: // 筛选
  394. showCollects()
  395. case 3: // 下一步
  396. if selectedData == nil {
  397. cShowHUB(superView: nil, msg: isAssetImage ? "请选择图片" : "请选择视频")
  398. return
  399. }
  400. imageManager.requestImage(for: (selectedData?.asset)!, targetSize: itemSize, contentMode: .aspectFill, options: nil) { [weak self] image, _ in
  401. self?.selectedData?.image = image
  402. let vc = PQImageCropVC(image: (self?.selectedData?.image)!, aspectWidth: self?.videoWidth ?? 0.0, aspectHeight: self?.videoHeight ?? 0.0)
  403. vc.uploadData = self?.selectedData
  404. self?.navigationController?.pushViewController(vc, animated: true)
  405. }
  406. default:
  407. break
  408. }
  409. }
  410. @objc func showCollects() {
  411. if categoryData.count <= 0 {
  412. return
  413. }
  414. categoryCollectionView.reloadData()
  415. if categoryView.superview == nil {
  416. view.insertSubview(categoryView, belowSubview: navHeadImageView!)
  417. categoryView.addSubview(categoryCollectionView)
  418. showCategoryView()
  419. return
  420. }
  421. if categoryView.isHidden {
  422. showCategoryView()
  423. } else {
  424. cancelClick()
  425. }
  426. }
  427. @objc func showCategoryView() {
  428. categoryView.isHidden = false
  429. categoryView.alpha = 0
  430. view.bringSubviewToFront(categoryView)
  431. view.bringSubviewToFront(bottomView)
  432. UIView.animate(withDuration: 0.3, animations: {
  433. self.categoryCollectionView.frame = CGRect(x: 0, y: 0, width: cScreenWidth, height: self.categoryH)
  434. self.categoryView.alpha = 1
  435. }) { _ in
  436. }
  437. }
  438. @objc func cancelClick() {
  439. UIView.animate(withDuration: 0.3, animations: {
  440. self.categoryCollectionView.frame = CGRect(x: 0, y: -self.categoryH, width: cScreenWidth, height: self.categoryH)
  441. self.categoryView.alpha = 0
  442. }) { _ in
  443. self.categoryView.isHidden = true
  444. }
  445. }
  446. @objc func didBecomeActiveNotification() {
  447. if isJumpToAuthorization {
  448. loadLocalData()
  449. isJumpToAuthorization = false
  450. }
  451. }
  452. }
  453. extension PQUploadController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate {
  454. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection _: Int) -> Int {
  455. if collectionView == self.collectionView {
  456. return allPhotos == nil ? videoData.count : allPhotos.count
  457. }
  458. return categoryData.count
  459. }
  460. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  461. if collectionView == self.collectionView {
  462. let cell = PQSelecteVideoItemCell.selecteVideoItemCell(collectionView: collectionView, indexPath: indexPath)
  463. if videoData.count <= indexPath.item, allPhotos != nil {
  464. let itemData = PQUploadModel()
  465. // itemData.image = UIImage.init(named: "cut_place")
  466. let asset = allPhotos.object(at: indexPath.item)
  467. itemData.asset = asset
  468. itemData.duration = asset.duration
  469. videoData.append(itemData)
  470. }
  471. if videoData.count > indexPath.item {
  472. let itemData = videoData[indexPath.item]
  473. cell.uploadData = itemData
  474. if itemData.image == nil, itemData.asset != nil {
  475. cell.representedAssetIdentifier = itemData.asset?.localIdentifier
  476. imageManager.requestImage(for: itemData.asset!, targetSize: itemSize, contentMode: .aspectFill, options: nil) { image, info in
  477. if info?.keys.contains("PHImageResultIsDegradedKey") ?? false, "\(info?["PHImageResultIsDegradedKey"] ?? "0")" == "0", cell.representedAssetIdentifier == itemData.asset?.localIdentifier {
  478. if image != nil {
  479. itemData.image = image
  480. cell.videoImageView.image = image
  481. } else if image == nil, info?.keys.contains("PHImageResultIsInCloudKey") ?? false {
  482. let option = PHImageRequestOptions()
  483. option.isNetworkAccessAllowed = true
  484. option.resizeMode = .fast
  485. self.imageManager.requestImageData(for: itemData.asset!, options: option) { data, _, _, _ in
  486. if data != nil {
  487. let image = UIImage(data: data!)
  488. itemData.image = image
  489. cell.videoImageView.image = image
  490. }
  491. }
  492. }
  493. }
  494. }
  495. }
  496. } else {
  497. cell.uploadData = PQUploadModel()
  498. }
  499. return cell
  500. } else {
  501. let cell = PQAssetCategoryCell.assetCategoryCell(collectionView: collectionView, indexPath: indexPath)
  502. let itemData = categoryData[indexPath.item]
  503. let asset = itemData.categoryList.object(at: 0)
  504. if itemData.image == nil {
  505. cell.representedAssetIdentifier = asset.localIdentifier
  506. imageManager.requestImage(for: asset, targetSize: itemSize, contentMode: .aspectFill, options: nil) { image, info in
  507. if info?.keys.contains("PHImageResultIsDegradedKey") ?? false, "\(info?["PHImageResultIsDegradedKey"] ?? "0")" == "0", cell.representedAssetIdentifier == asset.localIdentifier {
  508. itemData.image = image
  509. cell.uploadData = itemData
  510. }
  511. }
  512. } else {
  513. cell.uploadData = itemData
  514. }
  515. cell.isSelected = indexPath == catagerySelectedIndex
  516. return cell
  517. }
  518. }
  519. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  520. if collectionView == self.collectionView {
  521. if videoData.count <= indexPath.item {
  522. return
  523. }
  524. let itemData = videoData[indexPath.item]
  525. let ratio = Float(itemData.asset?.pixelWidth ?? 0) / Float(itemData.asset?.pixelHeight ?? 1)
  526. if ratio < 0.4 || ratio > 4.2 {
  527. cShowHUB(superView: nil, msg: "暂不支持该比例的素材")
  528. return
  529. }
  530. if lastSeletedIndex != nil, lastSeletedIndex != indexPath {
  531. let lastData = videoData[lastSeletedIndex!.item]
  532. lastData.isSelected = false
  533. collectionView.reloadItems(at: [lastSeletedIndex!])
  534. }
  535. lastSeletedIndex = indexPath
  536. if itemData.isSelected {
  537. itemData.isSelected = false
  538. selectedData = nil
  539. } else {
  540. itemData.isSelected = true
  541. let itemData = videoData[indexPath.item]
  542. selectedData = itemData
  543. }
  544. collectionView.reloadItems(at: [indexPath])
  545. } else {
  546. videoData.removeAll()
  547. self.collectionView.setContentOffset(CGPoint.zero, animated: true)
  548. self.collectionView.reloadData()
  549. allPhotos = categoryData[indexPath.item].categoryList
  550. catagerySelectedIndex = indexPath
  551. selecteBtn.setTitle(categoryData[indexPath.item].title, for: .normal)
  552. selecteBtn.imagePosition(at: .right, space: cDefaultMargin / 2)
  553. lastSeletedIndex = nil
  554. if allPhotos.count <= 0 {
  555. emptyRemindView.frame = CGRect(x: 0, y: cDevice_iPhoneNavBarAndStatusBarHei, width: cScreenWidth, height: cScreenHeigth - cDevice_iPhoneNavBarAndStatusBarHei - cDevice_iPhoneTabBarHei)
  556. emptyRemindView.emptyData = emptyData
  557. emptyRemindView.isHidden = false
  558. } else {
  559. emptyRemindView.isHidden = true
  560. }
  561. self.collectionView.reloadData()
  562. cancelClick()
  563. }
  564. }
  565. func collectionView(_ collectionView: UICollectionView, layout _: UICollectionViewLayout, sizeForItemAt _: IndexPath) -> CGSize {
  566. if collectionView == self.collectionView {
  567. return CGSize(width: (cScreenWidth - cDefaultMargin) / 3, height: (cScreenWidth - cDefaultMargin) / 3)
  568. }
  569. return CGSize(width: collectionView.frame.width, height: cDefaultMargin * 8)
  570. }
  571. func collectionView(_ collectionView: UICollectionView, layout _: UICollectionViewLayout, minimumLineSpacingForSectionAt _: Int) -> CGFloat {
  572. if collectionView == self.collectionView {
  573. return cDefaultMargin / 2
  574. }
  575. return 0
  576. }
  577. func collectionView(_ collectionView: UICollectionView, layout _: UICollectionViewLayout, minimumInteritemSpacingForSectionAt _: Int) -> CGFloat {
  578. if collectionView == self.collectionView {
  579. return cDefaultMargin / 2
  580. }
  581. return 0
  582. }
  583. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  584. //这里是不是有用的?
  585. // if currentController() is PQUploadController || currentController() is PQImageSelectedController {
  586. // if scrollView == collectionView {
  587. // updateCachedAssets()
  588. // } else {
  589. // BFLog(message: "contentOffset = \(scrollView.contentOffset),contentSize = \(scrollView.contentSize)")
  590. // if scrollView.contentOffset.y > ((scrollView.contentSize.height - scrollView.frame.height) + cDefaultMargin * 10) {
  591. // cancelClick()
  592. // }
  593. // }
  594. // }
  595. }
  596. }
  597. extension PQUploadController: PHPhotoLibraryChangeObserver {
  598. func photoLibraryDidChange(_ changeInstance: PHChange) {
  599. // Change notifications may be made on a background queue. Re-dispatch to the
  600. // main queue before acting on the change as we'll be updating the UI.
  601. DispatchQueue.main.sync { [weak self] in
  602. // Check each of the three top-level fetches for changes.
  603. if allPhotos != nil, changeInstance.changeDetails(for: allPhotos) != nil {
  604. self?.categoryData.removeAll()
  605. self?.loadPhotoData()
  606. }
  607. }
  608. }
  609. }
  610. extension PQUploadController {
  611. /// 添加监听
  612. /// - Parameter playerItem: <#playerItem description#>
  613. /// - Returns: <#description#>
  614. func addPlayerItemObserver() {
  615. playerItem?.addObserver(self, forKeyPath: "status", options: .new, context: nil)
  616. }
  617. /// 移除监听
  618. /// - Returns: <#description#>
  619. func removePlayerItemObserver() {
  620. playerItem?.removeObserver(self, forKeyPath: "status")
  621. }
  622. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change _: [NSKeyValueChangeKey: Any]?, context _: UnsafeMutableRawPointer?) {
  623. if object is AVPlayerItem, keyPath == "status" {
  624. BFLog(message: "(object as! AVPlayerItem).status = \((object as! AVPlayerItem).status.rawValue)")
  625. PQLoadingHUB.shared.dismissHUB(superView: playerHeaderView)
  626. switch (object as! AVPlayerItem).status {
  627. case .unknown:
  628. break
  629. case .readyToPlay:
  630. break
  631. case .failed:
  632. break
  633. default:
  634. break
  635. }
  636. }
  637. }
  638. private func resetCachedAssets() {
  639. imageManager.stopCachingImagesForAllAssets()
  640. previousPreheatRect = .zero
  641. }
  642. private func updateCachedAssets() {
  643. if allPhotos != nil && allPhotos.count <= 0 {
  644. return
  645. }
  646. guard isViewLoaded, view.window != nil else { return }
  647. let visibleRect = CGRect(origin: collectionView.contentOffset, size: collectionView.bounds.size)
  648. let preheatRect = visibleRect.insetBy(dx: 0, dy: -0.5 * visibleRect.height)
  649. let delta = abs(preheatRect.midY - previousPreheatRect.midY)
  650. guard delta > view.bounds.height / 3 else { return }
  651. let (addedRects, removedRects) = differencesBetweenRects(previousPreheatRect, preheatRect)
  652. let addedAssets = addedRects
  653. .flatMap { rect in collectionView.indexPathsForElements(in: rect) }
  654. .map { indexPath in allPhotos.object(at: indexPath.item) }
  655. let removedAssets = removedRects
  656. .flatMap { rect in collectionView.indexPathsForElements(in: rect) }
  657. .map { indexPath in allPhotos.object(at: indexPath.item) }
  658. imageManager.startCachingImages(for: addedAssets,
  659. targetSize: itemSize, contentMode: .aspectFill, options: nil)
  660. imageManager.stopCachingImages(for: removedAssets,
  661. targetSize: itemSize, contentMode: .aspectFill, options: nil)
  662. previousPreheatRect = preheatRect
  663. }
  664. private func differencesBetweenRects(_ old: CGRect, _ new: CGRect) -> (added: [CGRect], removed: [CGRect]) {
  665. if old.intersects(new) {
  666. var added = [CGRect]()
  667. if new.maxY > old.maxY {
  668. added += [CGRect(x: new.origin.x, y: old.maxY,
  669. width: new.width, height: new.maxY - old.maxY)]
  670. }
  671. if old.minY > new.minY {
  672. added += [CGRect(x: new.origin.x, y: new.minY,
  673. width: new.width, height: old.minY - new.minY)]
  674. }
  675. var removed = [CGRect]()
  676. if new.maxY < old.maxY {
  677. removed += [CGRect(x: new.origin.x, y: new.maxY,
  678. width: new.width, height: old.maxY - new.maxY)]
  679. }
  680. if old.minY < new.minY {
  681. removed += [CGRect(x: new.origin.x, y: old.minY,
  682. width: new.width, height: new.minY - old.minY)]
  683. }
  684. return (added, removed)
  685. } else {
  686. return ([new], [old])
  687. }
  688. }
  689. }