Video saving and display in the iOS app.

| | August 7, 2015

I’m developing an app where user can record video and save it in the library/or custom album. I’m able to create the custom album and record the video and save it in the default photo library i.e., camera roll.. But i’m unable to save it to the custom album. and other thing is the saved videos in the album must be showed in the collection view of the app i.e., like the gallery view.. so that user can able to click on the videos for the play.. it might be in the grid view/ collection view or might be in the table view where the UIImage should should display the videos saved in the album..

Here’s code section which i used in the app development for creating the custom album.

ALAssetsLibrary* libraryFolder = [[ALAssetsLibrary alloc] init];
[libraryFolder addAssetsGroupAlbumWithName:@"HEP" resultBlock:^(ALAssetsGroup *group)
 {
     NSLog(@"Adding Folder:'My Album', success: %s", group.editable ? "Success" : "Already created: Not Success");
 } failureBlock:^(NSError *error)
 {
     NSLog(@"Error: Adding on Folder");
 }];

For recording the video in the app.

- (IBAction)StartRecord:(id)sender {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
    [self presentViewController:picker animated:YES completion:NULL];

}

For saving it in the album i.e., default photo album not the custom album i created.

-(BOOL)startMediaBrowserFromViewController:(UIViewController*)controller usingDelegate:(id )delegate {
    // 1 - Validations
    if (([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum] == NO)
        || (delegate == nil)
        || (controller == nil)) {
        return NO;
    }
    // 2 - Get image picker
    UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];
    mediaUI.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    mediaUI.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
    // Hides the controls for moving & scaling pictures, or for
    // trimming movies. To instead show the controls, use YES.
    mediaUI.allowsEditing = YES;
    mediaUI.delegate = delegate;
    // 3 - Display image picker
    [controller presentModalViewController:mediaUI animated:YES];
    return YES;
}

Since i’m beginner in iOS still learning.. I’m searching since for a month now. So please help me.

One Response to “Video saving and display in the iOS app.”

  1. First create your album folder with below code & make sure this code paste in AppDelegate.m file didFinishLaunchingWithOptions method :

    ALAssetsLibrary* libraryFolderSozialConnectVideo = [[ALAssetsLibrary alloc] init];
    [libraryFolderSozialConnectVideo addAssetsGroupAlbumWithName:@"HEP" resultBlock:^(ALAssetsGroup *group)
     {
         NSLog(@"HEP Folder Created");
     } failureBlock:^(NSError *error) {
         NSLog(@"Error: Adding on Folder");
     }];
    

    Now, use below function for download your video in HEP folder :

    - (void)downloadVideo:(NSURL *)videoURL {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"Downloading Started");
        NSData *urlData = [NSData dataWithContentsOfURL:videoURL];
        if (urlData) {
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"thefile.mp4"];
            [urlData writeToFile:filePath atomically:YES];
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
                [self.library saveVideo:[NSURL fileURLWithPath:filePath] toAlbum:@"HEP" completion:^(NSURL *assetURL, NSError *error)
                 {
                     NSLog(@"Success downloaded");
                 } failure:^(NSError *error) {
                     NSLog(@"Error : %@", [error localizedDescription]);
                     [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
                     [self.navigationController popViewControllerAnimated:YES];
                 }];
            });
        }
    }); }
    

    for videoURL in your didFinishPickingMediaWithInfo method call above function with below code :

    self.library = [[ALAssetsLibrary alloc] init];
    [self downloadVideo:[info valueForKey:UIImagePickerControllerMediaURL]];
    

    I hope this works . . .

Leave a Reply