James
James

Reputation: 35

Can't save video to Parse in swift

I am simply trying to have the user take a short video in the application, and when the video is recorded, the app would send it to the Parse back-end once the user clicks 'Use Video' using UIImagePickerController.

I am able to save the video locally in the idFinishPickingMediaWithInfo with

UISaveVideoAtPathToSavedPhotosAlbum(pathString!, self, nil, nil)

But I can't find anything online to save the video to Parse.

The line that that is running the error is

let videoFile:PFFile = PFFile(data: videoData)

With error handler - Cannot convert value of type 'String?' to expected argument type 'NSData'

I believe it has something to do with this line above it causing the error:

let videoData = tempImage.relativePath

I can't find code to get it working. Any help would me amazing!

@IBAction func recordAction(sender: AnyObject) {

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {

    print("Camera Avilable")

    let imagePicker = UIImagePickerController()

        imagePicker.delegate = self
        imagePicker.sourceType = .Camera
        imagePicker.mediaTypes = [kUTTypeMovie as String]
        imagePicker.videoMaximumDuration = 180 // Perhaps reduce 180 to 120
        imagePicker.videoQuality = UIImagePickerControllerQualityType.TypeMedium
        imagePicker.allowsEditing = false

        imagePicker.showsCameraControls = true

        self.presentViewController(imagePicker, animated: true, completion: nil)

    } else {

        print("Camera Unavailable")

    }
}

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

    let tempImage = info[UIImagePickerControllerMediaURL] as! NSURL!
    let pathString = tempImage.relativePath
    self.dismissViewControllerAnimated(true, completion: nil)


    let videoData = tempImage.relativePath
    let videoFile:PFFile = PFFile(data: videoData)

    PFUser.currentUser()?.setObject(videoFile, forKey: "videoFile")
    PFUser.currentUser()?.saveInBackground()

}

Upvotes: 1

Views: 796

Answers (2)

Sergey Salnikov
Sergey Salnikov

Reputation: 321

I have no experience with Parse, but the documentation suggests that the PFFile(data:) constructor indeed expects the contents of the file, and you give the name. You likely want to use PFFile(name: nil, contentsAtPath: pathString!).

Note that you set pathString and videoData to the same value, which is the path and not the data.

More importantly, note that if you simply load the file with NSData(contentsOfFile: pathString!) as RYANCOAL9999 suggested, you are putting the entire video file into RAM. You could use NSData(contentsOfFile: pathString!, options: .DataReadingMappedIfSafe), but I believe it's best to simply delegate the entire file copying process to the libraries.

Upvotes: 0

RYANCOAL9999
RYANCOAL9999

Reputation: 47

let videoData = NSData(contentsOfFile:tempImage.relativePath)

let videoFile:PFFile = PFFile(name:"yourfilenamewithtype", data:videoData)

file.saveInBackgroundWithBlock({(succeeded:Bool,error:NSError?)

//Handle success or failure here.

},progressBlock:{(percentDone: Int32) -> Void in

//Update your progress spinner here.percentDone will be between 0 and 100.

})

yourfilenamewithtype like this,resume.txt

The method is the try to make file to NSdata.

Upvotes: 1

Related Questions