O'Reilly logo

iOS 7 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

15.5. Storing Videos in the Photo Library

Problem

You want to store a video accessible through a URL, such as a video in your application bundle, to the photo library.

Solution

Use the writeVideoAtPathToSavedPhotosAlbum:completionBlock: instance method of ALAssetsLibrary:

#import "AppDelegate.h"
#import <AssetsLibrary/AssetsLibrary.h>

@interface AppDelegate ()
@property (nonatomic, strong) ALAssetsLibrary *assetsLibrary;
@end

@implementation AppDelegate

- (BOOL)            application:(UIApplication *)application
  didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

    self.assetsLibrary = [[ALAssetsLibrary alloc] init];

    NSURL *videoURL = [[NSBundle mainBundle] URLForResource:@"MyVideo"
                                              withExtension:@"MOV"];

    if (videoURL != nil){
        [self.assetsLibrary
         writeVideoAtPathToSavedPhotosAlbum:videoURL
         completionBlock:^(NSURL *assetURL, NSError *error) {

             if (error == nil){
                 NSLog(@"no errors happened");
             } else {
                 NSLog(@"Error happened while saving the video.");
                 NSLog(@"The error is = %@", error);
             }

         }];
    } else {
        NSLog(@"Could not find the video in the app bundle.");
    }

    return YES;
}

Discussion

The Assets Library framework is a convenient bridge between developers and the photo library. As mentioned in Recipe 15.6, the iOS SDK provides you with built-in GUI components that you can use to access the contents of the photo library. However, you might sometimes require direct access to these contents. In such instances, you can use the Assets Library framework.

After allocating and initializing the Assets Library ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required