saving a movie in swift

1.3k views Asked by At

I used the following code and did not succeed to save the movie Please tell me where am I wrong:

On top I used,

import UIKit
import MobileCoreServices
import Foundation

class RecordingViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate

The code itself is activated from a button,

//Recording a video
    @IBAction func recordANewVideoMessage(sender: UIButton) {

        //Initiate camera
        let picker = UIImagePickerController()
    picker.delegate = self
    if UIImagePickerController.isSourceTypeAvailable(.Camera)
    {

        picker.sourceType = .Camera

        if let availableTypes = UIImagePickerController.availableMediaTypesForSourceType(.Camera) {


            if (availableTypes as NSArray).containsObject(kUTTypeMovie) {

                /* Set up the camera defaults, movie. no editing, 3 minutes max - consider presenting time during recording, medium quality */
                picker.mediaTypes = [kUTTypeMovie]
                picker.allowsEditing = false
                picker.videoMaximumDuration = 180 // Perhaps reduce 180 to 120
                picker.videoQuality = UIImagePickerControllerQualityType.TypeMedium
                presentViewController(picker, animated: true, completion: nil) // presents the camera for the user


            }
        }
    }



//In case the user cancels the movie
func imagePickerControllerDidCancel(UIImagePickerController) {

        presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
    }


func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {


    let uniqueVideoName = "/temporaryVideoName" //will be altered when we have something unique for the user

    var video = info[UIImagePickerControllerReferenceURL] as! NSURL

    let urlVideo = video.URLByAppendingPathComponent("\(uniqueVideoName)"+".mpeg4").absoluteString


    if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(urlVideo))
        {
            UISaveVideoAtPathToSavedPhotosAlbum(urlVideo, nil, nil, nil)
        }

        presentingViewController?.dismissViewControllerAnimated(true, completion: nil)

        }

}
1

There are 1 answers

0
Alonzo On

Here is a way to save a video to the camera roll in swift:

import Photos

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    self.dismissViewControllerAnimated(true, completion: nil)

    if let referenceURL = info[UIImagePickerControllerReferenceURL] as? NSURL {
        let fetchResult = PHAsset.fetchAssetsWithALAssetURLs([referenceURL], options: nil)
        if let phAsset = fetchResult.firstObject as? PHAsset {
            PHImageManager.defaultManager().requestAVAssetForVideo(phAsset, options: PHVideoRequestOptions(), resultHandler: { (asset, audioMix, info) -> Void in
                if let asset = asset as? AVURLAsset {
                    let videoData = NSData(contentsOfURL: asset.URL)

                    // write the video to the temp directory
                    let videoPath = NSTemporaryDirectory() + "tmpMovie.MOV"
                    let videoURL = NSURL(fileURLWithPath: videoPath)
                    videoData?.writeToURL(videoURL, atomically: true)

                    let assetLibrary = ALAssetsLibrary()
                    assetLibrary.writeVideoAtPathToSavedPhotosAlbum(videoURL, completionBlock: { (url, error) -> Void in
                        if error == nil {
                            print("saved")
                        }
                        else {
                            print(error)
                        }
                    })
                }
            })
        }
    }
}