Swift: save video from NSURL to user camera roll

前端 未结 6 618
天命终不由人
天命终不由人 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:08

    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)
        }
    }
    
    0 讨论(0)
  • 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)
        }
    }
    
    0 讨论(0)
  • 2020-12-13 14:17

    deprecated as of iOS 9

    1: import AssetsLibrary

    import AssetsLibrary
    

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

    ALAssetsLibrary().writeVideoAtPathToSavedPhotosAlbum(outputFileURL, completionBlock: nil)
    
    0 讨论(0)
  • 2020-12-13 14:20

    Try this instead for saving video in photo library in swift 4.2 and above

    func requestAuthorization(completion: @escaping ()->Void) {
            if PHPhotoLibrary.authorizationStatus() == .notDetermined {
                PHPhotoLibrary.requestAuthorization { (status) in
                    DispatchQueue.main.async {
                        completion()
                    }
                }
            } else if PHPhotoLibrary.authorizationStatus() == .authorized{
                completion()
            }
        }
    
    
    
    func saveVideoToAlbum(_ outputURL: URL, _ completion: ((Error?) -> Void)?) {
            requestAuthorization {
                PHPhotoLibrary.shared().performChanges({
                    let request = PHAssetCreationRequest.forAsset()
                    request.addResource(with: .video, fileURL: outputURL, options: nil)
                }) { (result, error) in
                    DispatchQueue.main.async {
                        if let error = error {
                            print(error.localizedDescription)
                        } else {
                            print("Saved successfully")
                        }
                        completion?(error)
                    }
                }
            }
        }
    

    Use of function

    self.saveVideoToAlbum(/* pass your final url to save */) { (error) in
                            //Do what you want 
                        }
    

    Don't forgot to import Photos and add Privacy - Photo Library Usage Description to your info.plist

    0 讨论(0)
  • 2020-12-13 14:25

    To save video from NSURL to user camera roll

    func video(videoPath: NSString, didFinishSavingWithError error: NSError?, contextInfo info: AnyObject) 
     {
        if let _ = error {
           print("Error,Video failed to save")
        }else{
           print("Successfully,Video was saved")
        }
    }
    
    
    
    
    
    
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    
        if let conversationField = self.conversation {
    
          if (mediaType?.isEqual((kUTTypeMovie as NSString) as String))!
            {
                let theVideoURL: URL? = (info[UIImagePickerControllerMediaURL] as? URL)
    
                if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum((theVideoURL?.path)!))
                {
                    UISaveVideoAtPathToSavedPhotosAlbum((theVideoURL?.path)!, self, #selector(ConversationDetailsViewController.video(videoPath:didFinishSavingWithError:contextInfo:)), nil)
                }   
       }
       self.dismiss(animated: true, completion: nil)
    }
    

    Reference from:: https://www.raywenderlich.com/94404/play-record-merge-videos-ios-swift

    0 讨论(0)
  • 2020-12-13 14:25

    Just use it and paste your video's url:

    PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in
    
        let createAssetRequest: PHAssetChangeRequest = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(NSURL(string: /* your url */)!)!
        createAssetRequest.placeholderForCreatedAsset
    
        }) { (success, error) -> Void in
            if success {
    
                //popup alert success
            }
            else {
               //popup alert unsuccess
            }
    }
    
    0 讨论(0)
提交回复
热议问题