Hi,
The tutorial will help you to set an image on collectionView Cell and when you click on any image play the video.
//
//  ViewController.m
//  YouTubeVideo
//
//  Created by lalit on 4/7/15.
//  Copyright (c) 2015 lalit. All rights reserved.
//
#import "ViewController.h"
#import <XCDYouTubeKit/XCDYouTubeKit.h>
static int count;
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    NSArray *urlArray;
     NSMutableArray *photoArray;
    NSURL *videoURL;
}
@property (strong, nonatomic) MPMoviePlayerController *videoController;
@property (weak, nonatomic) IBOutlet UICollectionView *colView;
-(void)playVideo:(int)index;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
  photoArray = [NSMutableArray arrayWithObjects:@"like.png",@"like.png",@"like.png",@"like.png",@"like.png",@"like.png",@"like.png",@"like.png",@"like.png",@"like.png", nil];
    urlArray = [NSArray arrayWithObjects:@"https://www.youtube.com/watch?v=Ji4h77ZrMc8",@"https://www.youtube.com/watch?v=Ji4h77ZrMc8",@"https://www.youtube.com/watch?v=Ji4h77ZrMc8",@"https://www.youtube.com/watch?v=Ji4h77ZrMc8",@"https://www.youtube.com/watch?v=Ji4h77ZrMc8",@"https://www.youtube.com/watch?v=Ji4h77ZrMc8",@"https://www.youtube.com/watch?v=Ji4h77ZrMc8",@"https://www.youtube.com/watch?v=Ji4h77ZrMc8",@"https://www.youtube.com/watch?v=Ji4h77ZrMc8",@"https://www.youtube.com/watch?v=Ji4h77ZrMc8", nil];
    count = 10;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self
               action:@selector(aMethod:)
     forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"Refresh" forState:UIControlStateNormal];
    button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
    [self.colView addSubview:button];
}
- (void)aMethod:(UIButton*)button
{
    count = count + 10;
    if(count >[photoArray count])
    {
        count =[photoArray count];
    }
    [self.colView reloadData];
}
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
        return 1;
    }
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
        return photoArray.count;
    }
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *identifier = @"Cell";
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
        UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
        recipeImageView.image = [UIImage imageNamed:[photoArray objectAtIndex:indexPath.row]];
        return cell;
    }
    -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
    {
        [self playVideo : (indexPath.row)];
    }
    -(void)playVideo:(int)index
    {
        NSString *videoString = urlArray[index];
        NSLog(@"data%@",videoString);
        XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:videoString];
        [self presentMoviePlayerViewControllerAnimated:videoPlayerViewController];
    }
    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
        return CGSizeMake(130.f, 95.f);
    }
    @end
In this program I have use a singleUrl for all video but you can use different Url for different image.
Add the below mention pods 
<XCDYouTubeKit/XCDYouTubeKit.h>
and import the framework 
<MediaPlayer/MediaPlayer.h>
                       
                    
1 Comment(s)