PublishEditVideoContentViewController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. //
  2. // PublishEditContentViewController.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2019/11/9.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. //
  8. import UIKit
  9. class PublishEditVideoContentViewController: BaseViewController {
  10. var isRenewEdit : Bool = false
  11. /// 合成导出路径
  12. var outputPath : String?
  13. /// 发布模型
  14. var publishManagerModel : PublishManagerModel?
  15. /// 视频路径模型
  16. var publishManagerVideoModel : PublishManagerVideoModel?
  17. /// 发布内容
  18. var publishManagerContentModel : PublishManagerContentModel?
  19. /// 话题
  20. var publishManagerTopicModel : PublishManagerTopicModel?
  21. /// 定位
  22. var publishManagerLocationModel : PublishManagerLocationModel?
  23. override func viewDidLoad() {
  24. super.viewDidLoad()
  25. setVideoCoverImagePath()
  26. setupViews()
  27. setupLayouts()
  28. setupData()
  29. }
  30. func setVideoCoverImagePath() {
  31. if publishManagerModel == nil {
  32. // 清除图片路径
  33. PathURLManager.clearPath(pathURL:
  34. PathURLManager.projectPicturesPath(parentURL:
  35. PathURLManager.projectResourcesPath(parentURL:
  36. PathURLManager.projectRecordssPath(parentURL:
  37. PathURLManager.projectDocumentPath())))!)
  38. //设置录制视频封面输出路径
  39. let outCoverImagePathURL =
  40. PathURLManager.projectPathExtensionPng(parentURL:
  41. PathURLManager.projectFilePath(fileName:
  42. PathURLManager.randomString(), parentURL:
  43. PathURLManager.projectDateFormatPath(dateFormat: "yyyyMM", parentURL:
  44. PathURLManager.projectPicturesPath(parentURL:
  45. PathURLManager.projectResourcesPath(parentURL:
  46. PathURLManager.projectRecordssPath(parentURL:
  47. PathURLManager.projectDocumentPath()))))))!
  48. // 视频路径
  49. let asset = AVURLAsset.init(url: URL(fileURLWithPath: outputPath ?? ""))
  50. let gen = AVAssetImageGenerator.init(asset: asset)
  51. gen.appliesPreferredTrackTransform = true
  52. let time = CMTimeMakeWithSeconds(0, preferredTimescale: Int32(1 * NSEC_PER_SEC))
  53. var actualTime : CMTime = CMTimeMakeWithSeconds(0, preferredTimescale: 0)
  54. do {
  55. let image = try gen.copyCGImage(at: time, actualTime: &actualTime)
  56. //旋转修正
  57. let imageNew = UIImage.rotate(aImage: UIImage(cgImage: image))
  58. let imgData: Data? = UIImage.pngData(imageNew)()
  59. do {
  60. try imgData?.write(to: outCoverImagePathURL)
  61. }
  62. catch {
  63. SwiftProgressHUD.shared().showText("封面写入失败")
  64. NXLLog("存入失败")
  65. self.navigationController?.popViewController(animated: true)
  66. return
  67. }
  68. } catch {
  69. SwiftProgressHUD.shared().showText("封面写入失败")
  70. NXLLog("错误")
  71. self.navigationController?.popViewController(animated: true)
  72. return
  73. }
  74. publishManagerVideoModel = PublishManagerVideoModel()
  75. publishManagerVideoModel?.outVideoPath = outputPath
  76. publishManagerVideoModel?.outVideoPathURL = URL(fileURLWithPath: outputPath ?? "")
  77. publishManagerVideoModel?.outCoverImagePath = outCoverImagePathURL.path
  78. publishManagerVideoModel?.outCoverImagePathURL = outCoverImagePathURL
  79. //发布内容
  80. publishManagerContentModel = PublishManagerContentModel()
  81. // 话题
  82. publishManagerTopicModel = PublishManagerTopicModel()
  83. // 定位
  84. publishManagerLocationModel = PublishManagerLocationModel()
  85. }else {
  86. // 视频路径
  87. publishManagerVideoModel = publishManagerModel?.video
  88. //发布内容
  89. publishManagerContentModel = publishManagerModel?.content
  90. // 话题
  91. publishManagerTopicModel = publishManagerModel?.topic
  92. // 定位
  93. publishManagerLocationModel = publishManagerModel?.location
  94. }
  95. }
  96. override func setupViews() {
  97. navigationBar.wr_setBottomLineHidden(hidden: true)
  98. view.addSubview(publishButton)
  99. view.addSubview(tableView)
  100. }
  101. override func setupLayouts() {
  102. publishButton.snp.makeConstraints { (make) in
  103. make.centerY.equalTo(navigationBar.leftButton)
  104. make.right.equalTo(-14)
  105. make.width.equalTo(64)
  106. make.height.equalTo(26)
  107. }
  108. tableView.snp.makeConstraints { (make) in
  109. make.top.equalTo(kNavBarTotalHeight)
  110. make.left.right.bottom.equalToSuperview()
  111. }
  112. }
  113. override func setupData() {
  114. publishButton.rx.tap.subscribe(onNext: { [weak self] (data) in
  115. self?.publishAction()
  116. }).disposed(by: disposeBag)
  117. }
  118. lazy var publishButton: UIButton = {
  119. let publishButton = UIButton()
  120. publishButton.setBackgroundImage(UIImage.imageWithColor(color: kThemeColor), for: UIControl.State.normal)
  121. publishButton.setBackgroundImage(UIImage.imageWithColor(color: kd8d8d8Color), for: UIControl.State.disabled)
  122. publishButton.setTitle("发布", for: UIControl.State.normal)
  123. publishButton.setTitleColor(kffffffColor, for: UIControl.State.normal)
  124. publishButton.titleLabel?.font = kMediumFont13
  125. publishButton.cornerRadius = 13
  126. publishButton.masksToBounds = true
  127. return publishButton
  128. }()
  129. lazy var tableView: UITableView = {
  130. let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.grouped)
  131. tableView.separatorStyle = .none
  132. tableView.backgroundColor = kffffffColor
  133. tableView.dataSource = self
  134. tableView.delegate = self
  135. return tableView
  136. }()
  137. typealias POPClosure = () -> Void
  138. var popClosure : POPClosure?
  139. override func didMove(toParent parent: UIViewController?) {
  140. if !(parent != nil) {
  141. if let popClosure = popClosure {
  142. popClosure()
  143. }
  144. }
  145. }
  146. func publishAction() {
  147. if publishManagerContentModel?.detailTitle?.count == 0 || publishManagerContentModel?.detailTitle == nil || publishManagerContentModel?.detailTitle == "" {
  148. SwiftProgressHUD.shared().showText("还没输入描述内容")
  149. return
  150. }
  151. if publishManagerTopicModel?.topics?.isEmpty ?? true {
  152. SwiftProgressHUD.shared().showText("还没选择话题哟")
  153. return
  154. }
  155. if publishManagerModel == nil {
  156. publishManagerModel = PublishManagerModel()
  157. }
  158. publishManagerModel?.publishType = .video
  159. publishManagerModel?.video = publishManagerVideoModel
  160. publishManagerModel?.content = publishManagerContentModel
  161. publishManagerModel?.topic = publishManagerTopicModel
  162. publishManagerModel?.location = publishManagerLocationModel
  163. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "DismissFromPublishEditVc"), object: publishManagerModel)
  164. if isRenewEdit {
  165. self.navigationController?.popViewController(animated: true)
  166. }else {
  167. self.dismissToRootViewController(animated: false) {
  168. baseTabbarViewController?.selectedIndex = 0
  169. }
  170. }
  171. }
  172. }
  173. // MARK: - tableView dataSource && delegate
  174. extension PublishEditVideoContentViewController : UITableViewDelegate, UITableViewDataSource {
  175. func numberOfSections(in tableView: UITableView) -> Int {
  176. return 1
  177. }
  178. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  179. return 5
  180. }
  181. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  182. switch indexPath.row {
  183. case 0:
  184. let cell = PublishEditVideoCoverImageTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  185. cell.publishManagerVideoModel = publishManagerVideoModel
  186. return cell
  187. case 1:
  188. let cell = PublishEditContentViewTitleCell.cellWith(tableView: tableView, indexPath: indexPath)
  189. cell.publishManagerContentModel = publishManagerContentModel
  190. cell.titleTextClosure = {
  191. [weak self] (text) in
  192. guard let strongSelf = self else { return }
  193. strongSelf.publishManagerContentModel?.title = text
  194. }
  195. return cell
  196. case 2:
  197. let cell = PublishEditContentViewDescribeCell.cellWith(tableView: tableView, indexPath: indexPath)
  198. cell.publishManagerContentModel = publishManagerContentModel
  199. cell.commentTextViewClosure = {
  200. [weak self] (text) in
  201. guard let strongSelf = self else { return }
  202. strongSelf.publishManagerContentModel?.detailTitle = text
  203. }
  204. return cell
  205. case 3:
  206. let cell = PublishNewEditTopicViewTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  207. cell.publishManagerTopicModel = publishManagerTopicModel
  208. return cell
  209. case 4:
  210. let cell = PublishNewEditLocationTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  211. cell.publishManagerLocationModel = publishManagerLocationModel
  212. return cell
  213. default:
  214. return UITableViewCell()
  215. }
  216. }
  217. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  218. switch indexPath.row {
  219. case 3:
  220. let vc = PublishAddTopicController()
  221. vc.selTopicModelArr = publishManagerTopicModel?.topics ?? []
  222. vc.selTopicsClosure = {
  223. [weak self] (topicMdlArr) in
  224. self?.publishManagerTopicModel?.topics = topicMdlArr
  225. self?.tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.none)
  226. }
  227. self.navigationController?.pushViewController(vc, animated: true)
  228. case 4:
  229. let vc = PublishAddAddressController()
  230. vc.selectLocationClosure = {
  231. [weak self] (address,latitude,longitude) in
  232. if address == "" && address == "" && longitude == "" {
  233. self?.publishManagerLocationModel?.address = nil
  234. self?.publishManagerLocationModel?.latitude = nil
  235. self?.publishManagerLocationModel?.longitude = nil
  236. } else {
  237. self?.publishManagerLocationModel?.address = address
  238. self?.publishManagerLocationModel?.latitude = latitude
  239. self?.publishManagerLocationModel?.longitude = longitude
  240. }
  241. self?.tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.none)
  242. }
  243. self.navigationController?.pushViewController(vc, animated: true)
  244. default:
  245. break
  246. }
  247. }
  248. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  249. switch indexPath.row {
  250. case 0:
  251. return 130
  252. case 1,3,4:
  253. return 50
  254. case 2:
  255. return 175
  256. default:
  257. return UITableView.automaticDimension
  258. }
  259. }
  260. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  261. return 0.000001
  262. }
  263. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  264. return nil
  265. }
  266. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  267. return 0.000001
  268. }
  269. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  270. return nil
  271. }
  272. }