Reputation: 89
In my app, user's are allowed to record videos. After they record videos they are appended into an array, and placed on a table view with their thumbnails so they can select the video and watch it. However, if i record a video and it goes on the table view, after i exit out of the app the video is gone.
What is the best way to save the video, without saving it to the users camera roll, and retrieve it later when they open the app again. I read about core data but heard it was not recommended. Is there a better way to do so?
Can someone please explain this to me, I am very bad dealing with directories. I barely have any experience with them.
I record a video to this file location here:
self.movieFileOutput.startRecording(toOutputFileURL: URL(fileURLWithPath:self.videoFileLocation()), recordingDelegate: self)
func videoFileLocation() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .long
let date = dateFormatter.string(from: Date())
return NSTemporaryDirectory().appending("videoFile\(date)")
}
Once the video is finished recording, I append it to this array.
var videosArray: [URL] = [URL]()
Then i get the thumbnail of each video using this method:
func getThumbnail(_ outputFileURL:URL) -> UIImage {
let clip = AVURLAsset(url: outputFileURL)
let imgGenerator = AVAssetImageGenerator(asset: clip)
let cgImage = try! imgGenerator.copyCGImage(
at: CMTimeMake(0, 1), actualTime: nil)
let uiImage = UIImage(cgImage: cgImage)
return uiImage
}
I then append it to this array here:
var thumbnails = [UIImage]()
How can i make these videos and their thumbnails not disappear when a user exits the app. I really need help as I am very frustrated.
Would the best way to be to save them to:
Can you tell me the best way and show me how to do this. I needed a detailed answer. Thank you.
Upvotes: 0
Views: 984
Reputation: 8563
I would recommend storing all the metadata for the recording in core-data, and all of the video and image data in the file system. In core-data store any meta-data that you may want to display in the tableview - length, title, creator, creation-date, modified-date, tag etc - even if it could be generated by loading the file - it is much faster to just load it from a database. Store the video and thumbnail in the documents directory (not the temporary directory) and store a RELATIVE path to the files in core-data. Make sure to use a relative and not a absolute path, as the app directory can change when the app is upgraded or restored via iTunes.
It seems that you are naming the files based on the creation date of the video. I would recommend against this. Date formatters are complex and can change based on locale or timezone or other system settings. I would recommend instead to name the file based on a UUID that you store in core-data.
To summarize:
Upvotes: 2