Swift:将video从NSURL保存到用户相机胶卷

我有一个NSURLtypes的variablesvideoURL

如果我打电话给println(videoURL)它会返回这样的内容: http://files.parsetfss.com/d540f71f-video.mp4 : http://files.parsetfss.com/d540f71f-video.mp4

我有一个button,应该采取这个videoURL并保存video到用户的相机胶卷。

我做的最好的是这样的:

 UISaveVideoAtPathToSavedPhotosAlbum(videoPath: String!, completionTarget: AnyObject!, completionSelector: Selector, contextInfo: UnsafeMutablePointer<Void>) 

虽然我什至不知道这是否会工作,我不知道如何将videoFile:NSURL转换为一个videoPath

任何帮助表示赞赏。

编辑:

以下是不成功的:

 UISaveVideoAtPathToSavedPhotosAlbum(videoURL.relativePath, self, nil, nil) 

AssetsLibrary已被弃用

1:导入照片

 import Photos 

2:使用此代码将video从url保存到相机库。

 PHPhotoLibrary.sharedPhotoLibrary().performChanges({ PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(nsUrlToYourVideo) }) { saved, error in if saved { let alertController = UIAlertController(title: "Your video was successfully saved", message: nil, preferredStyle: .Alert) let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil) alertController.addAction(defaultAction) self.presentViewController(alertController, animated: true, completion: nil) } } 

Swift 3

 PHPhotoLibrary.shared().performChanges({ PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: urlToYourVideo) }) { saved, error in if saved { let alertController = UIAlertController(title: "Your video was successfully saved", message: nil, preferredStyle: .alert) let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil) alertController.addAction(defaultAction) self.present(alertController, animated: true, completion: nil) } } 

接受的答案不再适用于Swift 3.0和iOS 10。

首先,您需要在应用程序的plist文件中设置以下权限:

隐私 – 照片库使用说明

提供一个string,提供给用户解释你为什么要求这个权限。

接下来,导入照片:

 import Photos 

最后,这里是Swift 3.0的更新代码:

 PHPhotoLibrary.shared().performChanges({ PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: fileURL) }) { saved, error in if saved { let alertController = UIAlertController(title: "Your video was successfully saved", message: nil, preferredStyle: .alert) let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil) alertController.addAction(defaultAction) self.present(alertController, animated: true, completion: nil) } } 

将video从NSURL保存到用户相机胶卷

 func video(videoPath: NSString, didFinishSavingWithError error: NSError?, contextInfo info: AnyObject) { if let _ = error { print("Error,Video failed to save") }else{ print("Successfully,Video was saved") } } func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { if let conversationField = self.conversation { if (mediaType?.isEqual((kUTTypeMovie as NSString) as String))! { let theVideoURL: URL? = (info[UIImagePickerControllerMediaURL] as? URL) if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum((theVideoURL?.path)!)) { UISaveVideoAtPathToSavedPhotosAlbum((theVideoURL?.path)!, self, #selector(ConversationDetailsViewController.video(videoPath:didFinishSavingWithError:contextInfo:)), nil) } } self.dismiss(animated: true, completion: nil) } 

参考从:: https://www.raywenderlich.com/94404/play-record-merge-videos-ios-swift

自iOS 9起弃用

1:导入AssetsLibrary

 import AssetsLibrary 

2:使用此代码将video从url保存到相机库。

 ALAssetsLibrary().writeVideoAtPathToSavedPhotosAlbum(outputFileURL, completionBlock: nil)