PQUploadController.swift 34 KB

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