Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: save video from NSURL to user camera roll

I have a variable videoURL of type NSURL.

If I call println(videoURL) it would return something like this: http://files.parsetfss.com/d540f71f-video.mp4

I have a button set up that should take this videoURL and save the video to the user's camera roll.

The best I have done is this:

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

While I'm not even sure if this will work or not, I can't figure out how to convert videoFile:NSURL into a videoPath.

Any help is appreciated on this.

Edit:

The following is unsuccessful:

UISaveVideoAtPathToSavedPhotosAlbum(videoURL.relativePath, self, nil, nil) 
like image 652
George Poulos Avatar asked Apr 07 '15 02:04

George Poulos


People also ask

How do I save a video to my gallery in Swift?

You cannot save it, because you do not have it. You are going to have to download that file first. When it has downloaded, use the download URL (the URL of the file on disk) and save that to the camera roll.


2 Answers

AssetsLibrary is deprecated

1: import Photos

import Photos 

2: Use this code to save video from url to camera library.

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 & Swift 4

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)     } } 
like image 181
Boris Avatar answered Oct 22 '22 18:10

Boris


The accepted answer no longer works with Swift 3.0 & iOS 10.

First, you need to set the following permission in your app's plist file:

Privacy - Photo Library Usage Description

Provide a string that is presented to the user explaining why you are requesting the permission.

Next, import photos:

import Photos 

Finally, here is the updated code for 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)     } } 
like image 28
CodeBender Avatar answered Oct 22 '22 18:10

CodeBender