作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo 信息: [String : Any]) {
// Save the video to the app directory
let videoURL = info[UIImagePickerControllerMediaURL] as! NSURL
let videoData = NSData(contentsOf: videoURL as URL)
let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let documentsDirectory: AnyObject = paths[0] as AnyObject
let dataPath = documentsDirectory.appendingPathComponent("/vid1.mp4")
videoData?.write(toFile: dataPath, atomically: false)
self.dismiss(animated: true, completion: nil)
}
@IBAction func playVideoAction(_ sender: Any)
{
let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let documentsDirectory: AnyObject = paths[0] as AnyObject
let dataPath = documentsDirectory.appendingPathComponent("/vid1.mp4")
let videoAsset = (AVAsset(url: NSURL(fileURLWithPath: dataPath) as URL))
let playerItem = AVPlayerItem(asset: videoAsset)
let player = AVPlayer(playerItem: playerItem)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true)
{
playerViewController.player!.play()
}
}
how to save video file into document directory
我使用了此链接中的代码,但随着 xcode 8/swift 3 的更新,它不再那么有用。被这个问题困扰有一段时间了。
最佳答案
您想要使用PHImageManager.requestExportSession(forVideo:options:)
。这将异步准备资源(包括在需要时下载),并创建一个可用于保存文件的 AVExportSession 。通过指定您想要原始内容并传递内容(如果可能),您应该可以获得最佳质量的视频。
var dataPath: URL {
let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let documentsDirectory = URL(fileURLWithPath: paths[0])
return documentsDirectory.appendingPathComponent("/vid1.mp4")
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let url = info[UIImagePickerControllerReferenceURL] as! URL
let assets = PHAsset.fetchAssets(withALAssetURLs: [url], options: nil)
let asset = assets.object(at: 0)
let options = PHVideoRequestOptions()
options.version = .original
PHImageManager.default().requestExportSession(forVideo: asset, options: options, exportPreset: AVAssetExportPresetPassthrough) { (exportSession, info) in
guard let session = exportSession else { return }
session.outputURL = self.dataPath
session.outputFileType = AVFileTypeMPEG4
session.exportAsynchronously {
DispatchQueue.main.async {
self.dismiss(animated: true, completion: nil)
}
}
}
}
如果您不想保存到磁盘而只想播放,则可以使用 requestPlayerItem
获取可在播放器中使用的 AVPlayerItem
:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let url = info[UIImagePickerControllerReferenceURL] as! URL
let assets = PHAsset.fetchAssets(withALAssetURLs: [url], options: nil)
let asset = assets.object(at: 0)
let options = PHVideoRequestOptions()
options.version = .original
PHImageManager.default().requestPlayerItem(forVideo: asset, options: options) { (playerItem, info) in
let player = AVPlayer(playerItem: playerItem)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
DispatchQueue.main.async {
self.dismiss(animated: true, completion: nil)
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
}
}
}
关于swift - 如何将视频保存到应用程序的目录并在 View Controller 中播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42101458/
我是一名优秀的程序员,十分优秀!