title: Saving video file to photo gallery using Swift
body: <p>I am coping one video of 20 seconds. I am facing to save that video on my photo gallery. After successfully video trimming I need to call this function can any one help me to convert below code in Swift. I have pasted both version of code, if any one can identify my mistake?</p>
  
  <p><strong>Objective C</strong></p>
  
  <pre><code>-(void)writeVideoToPhotoLibrary:(NSURL*)aURL
  {
      NSURL *url = aURL;
      NSData *data = [NSData dataWithContentsOfURL:url];
  
      // Write it to cache directory
      NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"file.mov"];
      [data writeToFile:path atomically:YES];
  
      NSLog(@"Path:%@",path);
  
      // After that use this path to save it to PhotoLibrary
  
      ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
      [library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:path] completionBlock:^(NSURL *assetURL, NSError *error) {
  
          if (error) {
              NSLog(@"%@", error.description);
          }else {
              NSLog(@"Done :)");
          }
  
      }];
  
  
  }
  </code></pre>
  
  <p><strong>Swift</strong></p>
  
  <pre><code> func SaveVideoToPhotoLibrary(outputFileURL: NSURL)
  
      {
  
          assetsLibrary = ALAssetsLibrary()
  
          let videoURL = outputFileURL as NSURL?
  
          if let library = assetsLibrary{
  
              if let url = videoURL{
  
                  library.writeVideoAtPathToSavedPhotosAlbum(url,
                      completionBlock: {(url: NSURL!, error: NSError!) in
  
                          print(url)
  
                          if let theError = error{
                              print("Error happened while saving the video")
                              print("The error is = \(theError)")
                          } else {
                              print("no errors happened")
                          }
  
                  })
              } else {
                  print("Could not find the video in the app bundle")
              }
  
          }
  
              }
  </code></pre>