1

I'm using this function to download videos to a file called downloads using alamofire. How would I edit it so it saves videos to the camera roll

    func downloadVideoToCameraRoll() {


    let destination: DownloadRequest.DownloadFileDestination = { _, response in
        let pathComponent = response.suggestedFilename!
        var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let directoryURL: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let folderPath: URL = directoryURL.appendingPathComponent("Downloads", isDirectory: true)
        let fileURL: URL = folderPath.appendingPathComponent(pathComponent)
        return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
    }
    Alamofire.download(firstId, method: .get, parameters: nil, encoding: JSONEncoding.default, to: destination)
        .downloadProgress(queue: DispatchQueue.global(qos: .utility)) { progress in
            self.progresss.setProgress(Float(progress.fractionCompleted), animated: true)
            //print("Progress: \(progress.fractionCompleted)")

        }
        .validate { request, response, temporaryURL, destinationURL in
            // Custom evaluation closure now includes file URLs (allows you to parse out error messages if necessary)
            return .success
        }
        .responseJSON { response in
            debugPrint(response)
            print(response.temporaryURL!)
            print(response.destinationURL!)
    }
Ahmed Al Abdulaal
  • 270
  • 1
  • 3
  • 16
  • Possible duplicate of [Swift: save video from NSURL to user camera roll](https://stackoverflow.com/questions/29482738/swift-save-video-from-nsurl-to-user-camera-roll) – nathan Jun 26 '17 at 05:14
  • What you can do is, get the destination Url of the downloaded video and use Photos library to save it in the camera roll. – Aaqib Hussain Jun 26 '17 at 05:15
  • [Try This](https://stackoverflow.com/questions/11131050/how-can-i-save-an-image-to-the-camera-roll) after receiving image in your app – Akash Singh Sisodia Jun 26 '17 at 05:38

2 Answers2

3

You can use this in swift 3:

PHPhotoLibrary.shared().performChanges({
   PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: urlToYourVideo)
}) { saved, error in
if saved {
    print("Saved")
}
}

Noted: Need to import Photos

Sour LeangChhean
  • 7,089
  • 6
  • 37
  • 39
0

Try the code below

func downloadVideoToCameraRoll() {


    let destination: DownloadRequest.DownloadFileDestination = { _, response in
        let pathComponent = response.suggestedFilename!
        var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let directoryURL: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]

        return (directoryURL, [.removePreviousFile, .createIntermediateDirectories])
    }
    Alamofire.download(firstId, method: .get, parameters: nil, encoding: JSONEncoding.default, to: destination)
        .downloadProgress(queue: DispatchQueue.global(qos: .utility)) { progress in
            self.progresss.setProgress(Float(progress.fractionCompleted), animated: true)
            //print("Progress: \(progress.fractionCompleted)")

        }
        .validate { request, response, temporaryURL, destinationURL in
            // Custom evaluation closure now includes file URLs (allows you to parse out error messages if necessary)
            return .success
        }
        .responseJSON { response in
            debugPrint(response)
            print(response.temporaryURL!)
            print(response.destinationURL!)
            saveVideoTo(destinationURL)
    }


    func saveVideoTo(_ videoUrl:Url?){

    if videoUrl != nil {
            PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in

            let createAssetRequest: PHAssetChangeRequest = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(NSURL(string: videoUrl)!)!
            createAssetRequest.placeholderForCreatedAsset

            }) { (success, error) -> Void in
                if success {
                //saved successfully 

                }
                else {
                 //error occured
                }
        }

    }

    }
Aravind A R
  • 2,674
  • 1
  • 15
  • 25
  • HI. I got 2 red warning in your code is : 'Use of undeclared type 'Url'' and 'Use of undeclared type 'destinationURL'' . Can you help? Tks – famfamfam Jan 19 '19 at 17:18