Search code examples
swiftxcodesaveuiimagepickercontroller

Save video in Photo Library Swift


I'm trying to figure out how to save a video in my Photo Library. To get started I tried to simply pick a video by using the UIImagePickerController and after picking it saving it again in the library using the UISaveVideoAtPathToSavedPhotosAlbum. Doesn't make much sense but I try to understand how saving videos work.

The following code does not work, as it does not save the video:

@IBAction func ChooseVideo(_ sender: Any) {
    let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self

    imagePickerController.mediaTypes = ["public.movie"]
    self.present(imagePickerController, animated: true, completion: nil)
    }

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    let videoURL = info[UIImagePickerController.InfoKey.mediaURL] as? URL
    print(UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(videoURL!.path))

    dismiss(animated: true, completion: {
        UISaveVideoAtPathToSavedPhotosAlbum(videoURL!.path, self,  #selector(self.video(_:didFinishSavingWithError:contextInfo:)), nil)
    })
}

I hope you can help me as I couldn't find much about saving videos.

Best regards, MB

Solution:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
{
    // *** store the video URL returned by UIImagePickerController *** //
    let videoURL = info[UIImagePickerController.InfoKey.mediaURL] as! URL

    // *** load video data from URL *** //
    let videoData = NSData(contentsOf: videoURL)

    // *** Get documents directory path *** //
    let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0]

    // *** Append video file name *** //

    print(paths)
    let dataPath = paths.appending("/videoFileName.mp4")

    // *** Write video file data to path *** //
    videoData?.write(toFile: dataPath, atomically: false)

    PHPhotoLibrary.shared().performChanges({
        PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: URL(fileURLWithPath: dataPath))
    }) { saved, error in
        if saved {
            let fetchOptions = PHFetchOptions()
            fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

            let fetchResult = PHAsset.fetchAssets(with: .video, options: fetchOptions).firstObject
            // fetchResult is your latest video PHAsset
            // To fetch latest image  replace .video with .image
        }
    }
}

Solution

    • Use following func to save video to documents directory

      func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) 
      {
          // *** store the video URL returned by UIImagePickerController *** //
          let videoURL = info[UIImagePickerControllerMediaURL] as! NSURL
      
          // *** load video data from URL *** //
          let videoData = NSData(contentsOfURL: videoURL)
      
          // *** Get documents directory path *** //
          let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]
      
          // *** Append video file name *** //
          let dataPath = documentsDirectory.stringByAppendingPathComponent("/videoFileName.mp4")
      
          // *** Write video file data to path *** //
          videoData?.writeToFile(dataPath, atomically: false)
      }
      
    • now save this video in photo gellary

      PHPhotoLibrary.shared().performChanges({
          PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: Your document directory file)
      }) { saved, error in
          if saved {
              let fetchOptions = PHFetchOptions()
              fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
      
              let fetchResult = PHAsset.fetchAssets(with: .video, options: fetchOptions).firstObject
              // fetchResult is your latest video PHAsset
              // To fetch latest image  replace .video with .image
          }
      }
      

    after it if you don't need then delete the image from document directory , I hope it will work for you ...:)