Swift: save video from NSURL to user camera roll

前端 未结 6 622
天命终不由人
天命终不由人 2020-12-13 13:40

I have a variable videoURL of type NSURL.

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

6条回答
  •  温柔的废话
    2020-12-13 14:10

    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)
        }
    }
    

提交回复
热议问题