首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仅保存未修剪的视频Xcode 12 swift 5

仅保存未修剪的视频Xcode 12 swift 5
EN

Stack Overflow用户
提问于 2020-10-18 19:18:45
回答 1查看 818关注 0票数 1

根据本教程,我设置了一个简单的“记录”UIButton,在这里,我被带到相机前,只能录制视频,在拍摄完视频之后,我被带到视频编辑器屏幕上,在那里我可以修剪。一旦修剪,我点击保存和视频成功保存到我的相机卷。然而,它只是保存未修剪的视频。(在下面更新,使用UIVideoEditorController,我现在得到两个未经编辑的视频保存)。

守则如下:

VideoHelper.swift

代码语言:javascript
复制
import MobileCoreServices
import UIKit
import AVFoundation

enum VideoHelper {
  static func startMediaBrowser(
    delegate: UIViewController & UINavigationControllerDelegate & UIImagePickerControllerDelegate,
    sourceType: UIImagePickerController.SourceType
  ) {
    guard UIImagePickerController.isSourceTypeAvailable(sourceType)
      else { return }

    let mediaUI = UIImagePickerController()
    mediaUI.sourceType = sourceType //checks to see if a movie exists on the camera roll, the camera itself, and the photo library
    mediaUI.mediaTypes = [kUTTypeMovie as String] //allows only movies to be selected
    mediaUI.allowsEditing = true
    mediaUI.delegate = delegate
    delegate.present(mediaUI, animated: true, completion: nil)
  }
}

RecordVideoViewController.swift

代码语言:javascript
复制
import UIKit
import MobileCoreServices //contain predefined constants such as kUTTypeMovie which lets you select only video
import Photos
import AVFoundation

class RecordVideoViewController: UIViewController {
  @IBAction func record(_ sender: AnyObject) {
    VideoHelper.startMediaBrowser(delegate: self, sourceType: .camera) //opens the image picker and chooses the camera itself
    
  
  
  }
}
//MARK: - UIImagePickerControllerDelegate
extension RecordVideoViewController: UIImagePickerControllerDelegate {
  func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
  dismiss(animated: true, completion: nil)
  
    guard
      let mediaType = info[UIImagePickerController.InfoKey.mediaType] as? String,
      mediaType == (kUTTypeMovie as String),
      
      //1 gives you the URL pointing to the video
      let url = info[UIImagePickerController.InfoKey.mediaURL] as? URL,
      
      //2 verify the app can save the file to the device's photo album
      UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(url.path)
      else { return }
    
    //launches video editor
    guard UIVideoEditorController.canEditVideo(atPath: url.path) else { return }
    
    let editor = UIVideoEditorController()
    
    editor.videoPath = url.path
    editor.videoMaximumDuration = 10.0
    editor.videoQuality = .typeIFrame1280x720
    
    present(editor, animated: true, completion: nil)
    
      
    //3 if it can, save it
    UISaveVideoAtPathToSavedPhotosAlbum(
      url.path,
      self,
      #selector(video(_:didFinishSavingWithError:contextInfo:)),
      nil)
    
    }
    //Displays an alert announcing whether the video file was saved or not, based on the error status
    @objc func video(
      _ videoPath: String,
      didFinishSavingWithError error: Error?,
      contextInfo info: AnyObject
    ) {
        
      let title = (error == nil) ? "Success" : "Error"
      let message = (error == nil) ? "Video was saved" : "Video failed to save"
        
      let alert = UIAlertController(
        title: title,
        message: message,
        preferredStyle: .alert)
      alert.addAction(UIAlertAction(
        title: "OK",
        style: UIAlertAction.Style.cancel,
        handler: nil))
      present(alert, animated: true, completion: nil)
    }
  
 
  
}
//MARK: - UINavigationControllerDelegate
extension RecordVideoViewController: UINavigationControllerDelegate {
  
}

//MARK: - UIVideoEditorControllerDelegate
extension RecordVideoViewController: UIVideoEditorControllerDelegate {
  
}

我必须接近,因为我有一个成功的视频,这是我只是我只需要修整的版本。

将RecordVideoViewController.swift更新为

我现在已经添加了UIVideoEditorDelegate,并实例化了委托,并设置了委托并实现了委托方法。现在,它产生了两个保存的视频,其中没有一个是编辑的。

代码语言:javascript
复制
import UIKit
import MobileCoreServices //contain predefined constants such as kUTTypeMovie which lets you select only video
import Photos
import AVFoundation

class RecordVideoViewController: UIViewController {
  @IBAction func record(_ sender: AnyObject) {
    VideoHelper.startMediaBrowser(delegate: self, sourceType: .camera) //opens the image picker and chooses the camera itself
    
  
  
  }
}
//MARK: - UIImagePickerControllerDelegate
extension RecordVideoViewController: UIImagePickerControllerDelegate {
  func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
  dismiss(animated: true, completion: nil)
  
    guard
      let mediaType = info[UIImagePickerController.InfoKey.mediaType] as? String,
      mediaType == (kUTTypeMovie as String),
      
      //1 gives you the URL pointing to the video
      let url = info[UIImagePickerController.InfoKey.mediaURL] as? URL,
      
      //2 verify the app can save the file to the device's photo album
      UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(url.path),
    
     //****2.5?  Totally of the rails fromt he tutorial on this one:*****
      UIVideoEditorController.canEditVideo(atPath:url.path)
      else { return }
    
  
  
    //Must instantiate the UIVideoEditorController with "let editor = UIVideoEditorController()" and set the instance's delegate via self as well as show the file path

      let editor = UIVideoEditorController()
      editor.delegate = self

      editor.videoPath = url.path
  
      //presents the UIVideoEditorController
      self.present(editor, animated: true)
      print(editor.modalPresentationStyle.rawValue)
}
      // The UIVideoController's interface on the phone shows cancel and save buttons, which "do not" dismiss the presented view.  Must do that in the implementation of the delegate methods, of which there are 3.  And all 3 methods must be told to dismiss the presented view.
      // Delegate Method #1 (didSaveEditedVideoToPath): Called when the system has finished saving an edited movie.  At this point, the the trimmed video has already been saved to a file in app's temporary directory
  func videoEditorController(_ editor: UIVideoEditorController, didSaveEditedVideoToPath path: String) {
  self.dismiss(animated:true)
  
}
  

  //Delegate Method #2 (videoEditorControllerDidCancel): Called when the user has cancelled a movie editing operation
  func videoEditorControllerDidCancel(_ editor: UIVideoEditorController) {
  self.dismiss(animated:true)
}

  //Delegate Method #3 (didFailWithError): Called when the video editor is unable to load or save a movie.  Important as things "can" fail at this point.  MMhmmm
  func videoEditorController(_ editor: UIVideoEditorController, didFailWithError error: Error) {
  self.dismiss(animated:true)

    
    //***back on the rails with the tutorial, hope this works***
    
  //3 if it can, save it
  UISaveVideoAtPathToSavedPhotosAlbum(
    editor.videoPath,
    self,
    #selector(video(_:didFinishSavingWithError:contextInfo:)),
    nil)
    
    }
  //Displays an alert announcing whether the video file was saved or not, based on the error status
  @objc func video(
    _ videoPath: String,
    didFinishSavingWithError error: Error?,
    contextInfo info: AnyObject
    ) {
        
    let title = (error == nil) ? "Success" : "Error"
    let message = (error == nil) ? "Video was saved" : "Video failed to save"
        
    let alert = UIAlertController(
      title: title,
      message: message,
      preferredStyle: .alert)
    alert.addAction(UIAlertAction(
      title: "OK",
      style: UIAlertAction.Style.cancel,
      handler: nil))
    present(alert, animated: true, completion: nil)
    }
  
 
  
}
//MARK: - UINavigationControllerDelegate
extension RecordVideoViewController: UINavigationControllerDelegate {
  
}

//MARK: - UIVideoEditorControllerDelegate
extension RecordVideoViewController: UIVideoEditorControllerDelegate {
  
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-18 19:25:55

iOS中的代码不会神奇地“等待”一些事情发生。所以,当你这么说的时候

代码语言:javascript
复制
let editor = UIVideoEditorController()
// ...
present(editor, animated: true, completion: nil)
UISaveVideoAtPathToSavedPhotosAlbum(

...you提供了编辑器,但是与其等待用户看到编辑器并进行修剪,不如立即继续保存原来的视频。

那你怎么等呢?好吧,看看UIVideoEditorController文档。它有一个https://developer.apple.com/documentation/uikit/uivideoeditorcontroller/1622341-delegate。必须设置该委托(没有视频编辑器控制器就不能使用视频编辑器控制器),并且必须在委托中实现委托方法。因此,该方法将被调用-在用户编辑之后!当它被称为,它是交给编辑的视频!所以这就是你要存钱的地方。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64417391

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档