Download a file from AVCapture using AFNetworking

I have a video that is captured using AVCapture and I'm trying to download from AFNetworking using Swift.

code:

let manager = AFHTTPRequestOperationManager() let url = "http://localhost/test/upload.php" var fileURL = NSURL.fileURLWithPath(string: ViewControllerVideoPath) var params = [ "familyId":locationd, "contentBody" : "Some body content for the test application", "name" : "the name/title", "typeOfContent":"photo" ] manager.POST( url, parameters: params, constructingBodyWithBlock: { (data: AFMultipartFormData!) in println("") var res = data.appendPartWithFileURL(fileURL, name: "fileToUpload", error: nil) println("was file added properly to the body? \(res)") }, success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in println("Yes thies was a success") }, failure: { (operation: AFHTTPRequestOperation!, error: NSError!) in println("We got an error here.. \(error.localizedDescription)") }) 

The code crashes above, I keep getting

 was file added properly to the body? false" 

note that ViewControllerVideoPath is a string containing the location of the video, which:

 "/private/var/mobile/Containers/Data/Application/1110EE7A-7572-4092-8045-6EEE1B62949/tmp/movie.mov" 

using println() .... The code above works when I download the file included in the directory and using:

  var fileURL = NSURL.fileURLWithPath(NSBundle.mainBundle().pathForResource("test_1", ofType: "mov")!) 

So definitely my PHP code is ok and the problem is loading this file saved on the device, what am I doing wrong here?

+6
source share
2 answers

Comments do not provide a full explanation, so here is more information;

NSBundle.mainBundle () refers to the path in the package file. The path in the simulator is different from the path of the application ... this is not what you want. There are a number of β€œfolders” that you can access, depending on your needs (private or shared / files that can be copied to the cloud). NSPathUtils.h gives a breakdown of the available paths. According to the conventions used by most, you should probably create a private path under your application by doing something like:

  - (NSURL *) applicationPrivateDocumentsDirectory{ NSURL *pathURL = [[self applicationLibraryDirecory]URLByAppendingPathComponent:@"MyApplicationName"]; return pathURL; 

}

  - (NSURL *) applicationLibraryDirecory{ return [[[NSFileManager defaultManager] URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject]; 

}

You can check if it exists, if not, create it ... then save your video files in this way and transfer this to your AVCapture as the location to store the file.

+2
source

Here is the code that can perform the following functions in swift.

1: Check the weather catalog exists or not. if it does not exist, then create a directory (the directory provided the name of the application) in the document directory folder.

2: Now we have a catalog of applications. therefore, the entire file that the application will write / read to / from this directory.

  let file = "file.txt" let directoryName = "XYZ" // Here "XYZ" is project name. var error : NSError? let filemgr = NSFileManager.defaultManager() let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) let documentsDirectory = dirPaths[0] as! String var dataPath = documentsDirectory.stringByAppendingPathComponent(directoryName) if !NSFileManager.defaultManager().fileExistsAtPath(dataPath) { NSFileManager.defaultManager().createDirectoryAtPath(dataPath, withIntermediateDirectories: false, attributes: nil, error: &error) } else { println("not creted or exist") } 

Now we have a directory, so you only need to write / read data from the directory.

how to write a file to a document directory in swift

 let filePath = dataPath.stringByAppendingPathComponent(file); let text = "some text" //writing text.writeToFile(filePath, atomically: false, encoding: NSUTF8StringEncoding, error: nil); 

How to read a file from a document directory.

 let filePath = dataPath.stringByAppendingPathComponent(file); // Read file let text2 = String(contentsOfFile: filePath, encoding: NSUTF8StringEncoding, error: nil) 

Output: enter image description here

Hope this helps you.

+1
source

Source: https://habr.com/ru/post/985782/


All Articles