In the earlier recipes, we saved images, after some filtering, to Gallery as user's photos. In this recipe, we will investigate how to create a video file from camera images and save it to Gallery.
We will use the Recipe12_CapturingVideo
project as a starting point, trying to add the possibility to save processed camera images as video. The source code can be found in the Recipe13_SavingVideo
folder in the code bundle that accompanies this book. For this recipe, you can't use Simulator, as it doesn't support camera.
One of the many features supported by the latest iOS devices is 1080p HD video recording. It is incredible, because it is the largest resolution for the majority of modern TVs. Also, the modern iOS devices support hardware encoding with the H.264 codec and QuickTime container (.mov
).
The following are the required steps:
recordVideo
property.Let's implement the described steps:
recordVideo
property in the viewDidLoad
method:videoCamera.recordVideo = YES;
-(IBAction)stopCaptureButtonPressed:(id)sender { [videoCamera stop]; NSString* relativePath = [videoCamera.videoFileURL relativePath]; UISaveVideoAtPathToSavedPhotosAlbum(relativePath, nil, NULL, NULL); //Alert window UIAlertView *alert = [UIAlertView alloc]; alert = [alert initWithTitle:@"Status" message:@"Saved to the Gallery!" delegate:nil cancelButtonTitle:@"Continue" otherButtonTitles:nil]; [alert show]; isCapturing = FALSE; }
At a high level, the main method of the CvVideoCamera
class (that captures frames and processes them) looks like the following:
while (flag) { if (self.delegate) { //Get current frame //Call process function if (self.recordVideo == YES) { //Add the image to video file } } }
In other words, the processImage
method will be called for each frame after calling the start
method. And each processed frame is saved to the resulting video file if the value of the recordVideo
variable is YES
.
When a user clicks on the Stop capture button, the stopCaptureButtonPressed
method is called. In this function, we have to call the stop
method initially. After that, the resulting video file becomes available in tmp
folder of the current application. You can copy it from device with some software, such as iFunBox. You can get the path to this file through the videoCamera.videoFileURL
property. In order to copy it to Gallery, we should call the UISaveVideoAtPathToSavedPhotosAlbum
function using the path to the generated file. After that, we can show an alert window with a notification message about video saving as done in the Capturing a video from camera (Simple) recipe.