ios – Record video with AVfoundation framework and save to iphone gallery


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

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

I'm using AVfoundation library.

Can anyone give some light?


You will need a few steps using the UIImagePickerController :


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>


You will need 2 delegates , both for the Controller that will be opened to capture the video. 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 the way this Controller will be opened, being a "modal" and after verifying the existence of the camera on 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 yourself however you want to use them. Based on your question, I've already included to open the front camera with controls enabled.

Saving the video to the library

And finally, in the didFinishPickingMediaWithInfo delegate method, you will get all the information and save the video in your library:

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

    if ([mediaType isEqualToString:@""]) {
        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 I haven't included the else , but you can do it there, which indicate that the video was not captured, there is no compatibility and it is not possible to save in the device's library, either because it was not authorized or for any other reason.

And this other delegate , just to close the camera if the action is cancelled:

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

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

Scroll to Top