简体   繁体   中英

save Capture session in documents directory using swift with avfoundation from device

Below snippet i am using to save video in document directory(NSdata received from AVCaptureMoviefileOutput)

func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) {
    delegate?.recordingStopped?()

   var data = NSData(contentsOfURL: outputFileURL)
    let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
    let documentsDirectory:AnyObject=paths[0]
    let dataPath=documentsDirectory.stringByAppendingPathComponent("/MyFolder")
    if (!NSFileManager.defaultManager().fileExistsAtPath(dataPath)) {
        NSFileManager.defaultManager() .createDirectoryAtPath(dataPath as String, withIntermediateDirectories: false, attributes: nil, error: nil)
    }

    var outputPathr = "\(dataPath)/TestproximityRwqxq.mp4"

    var success = data!.writeToFile(outputPathr as String, options: nil, error: nil)

}

Same code is working in simulator if used for other video (picking video from NSbundle and converting to NSdata), but not working with device,Please help

This Code is working Perfectly, We need to use device which has minimum iOS version 8.4(XCode 6.4) . device which has version below 8.4, is not getting the content of document directory using Swift2.

The above solution I have modified for swift5 , it is working for me.

func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {

    if (error != nil) {
        print("Error recording movie: \(error!.localizedDescription)")

    } else {
        if let data = NSData(contentsOf: outputFileURL) {

            let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
            let documentDirectory = paths[0]
            let docURL = URL(string: documentDirectory)!
            let dataPath = docURL.appendingPathComponent("/DIRECTORY_NAME")

            if !FileManager.default.fileExists(atPath: dataPath.absoluteString) {
              do {
                    try FileManager.default.createDirectory(atPath: dataPath.absoluteString, withIntermediateDirectories: true, attributes: nil)
                    self.directoryURL = dataPath
                    print("Directory created successfully-\(dataPath.path)")
                } catch let error as NSError{
                    print("error creating directory -\(error.localizedDescription)");
                }
            }

            let outputPath = "\(dataPath.path)/filename.mp4"
            let success = data.write(toFile: outputPath, atomically: true)
            // saving video into photos album.
            UISaveVideoAtPathToSavedPhotosAlbum(outputPath, nil, nil, nil)

        } 
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM