O'Reilly logo

iOS 5 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

11.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:

- (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.");
  }
  
  self.window = [[UIWindow alloc] initWithFrame:
                 [[UIScreen mainScreen] bounds]];
  
  self.window.backgroundColor = [UIColor whiteColor];
  [self.window makeKeyAndVisible];
  return YES;
}

In the example, assetsLibrary is a property of type ALAssetsLibrary. Although this example allocates and initializes this property in the app delegate, you do not necessarily have to use Assets Library objects in your app delegate. You can allocate, initialize, and use them anywhere in your application that you find most appropriate.

Discussion

The Assets Library framework is a convenient bridge between developers and the Photo Library. As mentioned ...

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