I'm working with filemanager. So far I'm able to initialize my directory but when I try to save data there, the error i get is "error: Cannot create file".There seems to be an issue with the way I'm creating my path because when I use a temporary url, I am able to save to the photos library.
Here's the working temporary url code
var tempURL: URL? {
let directory = NSTemporaryDirectory() as NSString
if directory != "" {
let path = directory.appendingPathComponent("video.mov")
return URL(fileURLWithPath: path)
}
return nil
}
Here's the code where I execute the recording
do {
let videoPath = "SavedVideos"
var videoDirectory = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
videoDirectory = videoDirectory.appending(path: videoPath)
videoDirectory = videoDirectory.appending(path: "000001")
videoDirectory = videoDirectory.appending(path: "video.mov")
movieOutput.startRecording(to: videoDirectory, recordingDelegate: self)
}
catch {
print("error recording to video directory: \(error)")
}
Again, changing startRecording(to:videoDirectory to startRecording(to: tempURL! , I able to record. Any insights on what I'm doing wrong?
CodePudding user response:
The problem is that you add directories to the path (SavedVideos
and 000001
) without creating them , so Insert createDirectory
like below
do {
var videoDirectory = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
videoDirectory = videoDirectory.appending(path: "SavedVideos/000001")
try FileManager.default.createDirectory(atPath: videoDirectory.path, withIntermediateDirectories: true, attributes: nil)
videoDirectory = videoDirectory.appending(path: "video.mov")
movieOutput.startRecording(to: videoDirectory, recordingDelegate: self)
} catch {
print(error.localizedDescription)
}