Procházet zdrojové kódy

fix: #trello(【公共页】裁剪的加载页用白色弹窗样式)

Melody před 5 roky
rodič
revize
1f68a4bc0e

+ 69 - 10
SuperShow/UI/EditVideo/CutVideo/WSSEditCutView.swift

@@ -25,6 +25,11 @@ class WSSEditCutView: UIView {
     fileprivate var imageGenerator: AVAssetImageGenerator
     fileprivate var imageArray = [UIImage]()
     fileprivate var videoDuration: Float = 0
+
+    fileprivate var exportSession: AVAssetExportSession?
+    fileprivate var exportSessionDisplayLink: CADisplayLink?
+    fileprivate var isUserCancel = false
+
     weak var playerView: LXMAVPlayerView? {
         didSet {
             playerView?.playerTimeDidChangeBlock = { [weak self] current, total in
@@ -95,6 +100,17 @@ class WSSEditCutView: UIView {
             }
 
         })
+
+        // --
+        NotificationCenter.default.reactive.notifications(forName: Notification.Name.kSVProgressHUDCacnel).take(duringLifetimeOf: self).observeValues { [weak self] _ in
+            self?.isUserCancel = true
+
+            if let exportSession = self?.exportSession {
+                exportSession.cancelExport()
+                self?.exportSession = nil
+                hideHud()
+            }
+        }
     }
 
     convenience init(movieURL: URL) {
@@ -105,6 +121,10 @@ class WSSEditCutView: UIView {
     required init?(coder aDecoder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
     }
+    
+    func releaseObjc() {
+        stopExportSessionDisplay()
+    }
 
     func cutedStartSeconds() -> TimeInterval {
         let startTime = TimeInterval(bottomView.sliderView.selectedStart()) * TimeInterval(videoDuration)
@@ -162,21 +182,28 @@ private extension WSSEditCutView {
             let timeRange = CMTimeRange(start: startTime, end: endTime)
 
             do {
-                showHud()
+                self.isUserCancel = false
                 let cutedVideo = try WSSMediaOperationTool.cutVideo(withVideoAsset: self.videoAsset, timeRange: timeRange)
                 let exportUrl = kOJSUserDocumentDirectory + "/" + "tmp_edit_video_cuted.mp4"
-                _ = WSSMediaOperationTool.exportVideo(withVideoAsset: cutedVideo, exportPath: URL(fileURLWithPath: exportUrl), finish: { [weak self] isSucess in
-                    hideHud()
-
-                    if isSucess {
-                        DispatchQueue.main.async(execute: {
+                let exportSession = WSSMediaOperationTool.exportVideo(withVideoAsset: cutedVideo, exportPath: URL(fileURLWithPath: exportUrl), finish: { [weak self] isSucess in
+                    DispatchQueue.main.async(execute: {
+                        self?.stopExportSessionDisplay()
+                        if isSucess {
+                            hideHud()
                             self?.saveActionHandle?(exportUrl, startTime.seconds, timeRange.duration.seconds)
-                        })
-                    } else {
-                        showHud(withOnlyText: "截取视频失败")
-                    }
+                        } else {
+                            guard let userCancel = self?.isUserCancel, !userCancel else {
+                                showHud(withOnlyText: "取消裁剪")
+                                return
+                            }
+                            showHud(withOnlyText: "截取视频失败")
+                        }
+                    })
                 })
 
+                self.exportSession = exportSession
+                self.setupExportSessionDisplayLink()
+
             } catch {
                 showHud(withOnlyText: "截取视频失败")
             }
@@ -218,6 +245,38 @@ extension WSSEditCutView: WSSEditCutSliderViewDelegate {
         let endTime = TimeInterval(bottomView.sliderView.selectedEnd()) * TimeInterval(videoDuration)
         cutTimeRangeChangeHandle?(startTime, endTime - startTime)
     }
+
+    // MARK: exportSession
+
+    func setupExportSessionDisplayLink() {
+        if exportSessionDisplayLink != nil {
+            exportSessionDisplayLink?.isPaused = true
+            exportSessionDisplayLink?.invalidate()
+            exportSessionDisplayLink = nil
+        }
+        let displayLink = CADisplayLink(target: self, selector: #selector(updateExportSessionProgress))
+        DispatchQueue.main.async {
+            displayLink.add(to: .current, forMode: .common)
+            self.exportSessionDisplayLink = displayLink
+        }
+    }
+
+    func stopExportSessionDisplay() {
+        if let displayLink = exportSessionDisplayLink {
+            displayLink.isPaused = true
+            displayLink.invalidate()
+            exportSessionDisplayLink = nil
+        }
+    }
+
+    @objc func updateExportSessionProgress() {
+        if let exportSession = exportSession {
+            let progress = exportSession.progress
+            showProgressCancelHub(progress: progress, status: "正在裁剪...")
+        } else {
+            stopExportSessionDisplay()
+        }
+    }
 }
 
 // MARK: - PublicMethod

+ 3 - 0
SuperShow/UI/EditVideo/WSSEditVideoViewController.swift

@@ -171,6 +171,9 @@ class WSSEditVideoViewController: MTViewController {
         pasterOverlayView.removeAllSubViews()
         pasterOverlayView.removeFromSuperview()
 
+        cleanDisplayLink()
+        cutView.releaseObjc() /// cutView中含有DisplayLink
+
         displayView.endProcessing()
         displayView.removeFromSuperview()
         decoder?.outputFilter.removeAllTargets()