How to show iPhone Video library in UITableview programatically


Use the following code in XCode


-(void)viewDidAppear:(BOOL)animated
{
    assetsCollection=[[NSMutableArray alloc] init];
 al =[[ALAssetsLibrary alloc] init];
    UIImage *viewImage;
    [al writeImageToSavedPhotosAlbum:[viewImage CGImage] orientation:(ALAssetOrientation)[viewImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
        if (error) {
            NSLog(@"error");
        } else {
            NSLog(@"url %@", assetURL);
            
            
        }  
    }];
    
    [al enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
    {
        if (group!=NULL)
        {
            [group setAssetsFilter:[ALAssetsFilter allVideos]];
            [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
             {
                 if (asset) {
                     NSLog(@"asset: %@", asset);
                     [assetsCollection addObject:asset];
                 }
             } ];
            
        }
        
            [self.tableView reloadData];
    }
                              failureBlock:^(NSError *error) 
    {
        NSLog(@"error enumerating AssetLibrary groups %@\n", error);
    }];
    if([UIApplication sharedApplication].statusBarHidden)
    {
        [UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleBlackOpaque;
        [UIApplication sharedApplication].statusBarHidden=NO;
    }

}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell==nil)
    {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
if(tableView.tag==0)
{
    ALAsset *asset=[assetsCollection objectAtIndex:indexPath.row];
    NSLog(@"Asset Collection : %@",assetsCollection);
    NSLog(@"Asset %@",[[asset defaultRepresentation] url]);
    [cell.iconView setImage:[UIImage imageWithCGImage:[asset thumbnail]]];
    [cell.cellLabel setText:[NSString stringWithFormat:@"Video %d", indexPath.row+1]];
    [cell.playButton setEnabled:YES];
    [cell.playButton setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
    [cell.playButton addTarget:self action:@selector(movieplay:) forControlEvents:UIControlEventTouchUpInside];
    [cell.playButton setTag:indexPath.row];
}
else
{
 
    [cell.cellLabel setText:@"Show Resume"];
}
return cell;
    
     
}


-(void)movieplay:(id)sender
{
    UIButton *button=(UIButton*)sender;
    ALAsset *asset=[assetsCollection objectAtIndex:button.tag];
    delegate.movieURL = [[asset defaultRepresentation] url];
    MPMoviePlayerViewController *controller=[[MPMoviePlayerViewController alloc] init];
    [self presentMoviePlayerViewControllerAnimated:controller];    
}




Comments

Popular posts from this blog

How to set background color of navigation bar in iPhone

Checking Device Info in ios

Some Interactive tips to take input form GUI Dialogs