Record video with AVfoundation framework and save in iphone gallery

3

I'm creating an app for iOS 8 that uses the front camera to record user's video until a certain action is called, and then saved to the gallery.

But so far I've only been able to do this with a photo (take successive photos and save in the gallery) and not with video.

I'm using the AVfoundation library.

Can anyone give some light?

    
asked by anonymous 29.04.2015 / 22:08

1 answer

1

You will need a few steps using UIImagePickerController :

Frameworks

Import the required frameworks into Linked Frameworks and Libraries :

  • AssetsLibrary.framework
  • MobileCoreServices.framework

And in your header file:

#import <MobileCoreServices/MobileCoreServices.h>
#import <AssetsLibrary/AssetsLibrary.h>

Delegates

It will require 2 delegates , both for the Controller that will open for video capture. Also in your header file, include both:

@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

Setting properties and opening the camera

Here you define some properties of the camera and how this Controller will open, being a "modal" and after verifying the existence of the camera in the device:

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
    UIImagePickerController *cameraPicker = [[UIImagePickerController alloc] init];

    [cameraPicker setModalPresentationStyle:UIModalPresentationCurrentContext];
    [cameraPicker setSourceType:UIImagePickerControllerSourceTypeCamera];
    [cameraPicker setMediaTypes:[NSArray arrayWithObjects:(NSString *)kUTTypeMovie, nil]];
    [cameraPicker setShowsCameraControls:YES];
    [cameraPicker setCameraDevice:UIImagePickerControllerCameraDeviceFront];
    [cameraPicker setDelegate:self];

    [self presentViewController:cameraPicker animated:YES completion:nil];
}

These properties you can define for yourself. Based on your question, I've already included to open the front camera with the controls enabled.

Saving the video in the library

Finally, in the delegate didFinishPickingMediaWithInfo method, you will receive all the information and save the video to your library:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

    if ([mediaType isEqualToString:@"public.movie"]) {
        NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
        BOOL compatible = UIVideoAtPathIsCompatibleWithSavedPhotosAlbum([videoURL path]);

        if (compatible) {
            ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

            if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:videoURL]) {
                [library writeVideoAtPathToSavedPhotosAlbum:videoURL
                                            completionBlock:^(NSURL *assetURL, NSError *error) {
                                                [picker dismissViewControllerAnimated:YES completion:nil];
                                            }];
            }
        }
    }
}

Note that here I did not include the else , but you can do that, which indicates if there was no video capture, there is no compatibility and it is not possible to save in the device library, either because it was not authorized or any another reason.

And that other delegate , just to close the camera if the action is canceled:

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}

That's it. I made a very simple project here, if there is need I can make it available for download and you test it from there.

    
30.04.2015 / 15:07