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 ...
Get iOS 5 Programming Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.