Divyesh Dobariya
Divyesh Dobariya

Reputation: 125

How to display video from ALAsset to UICollectionview Cell

I tried this code to add all ALAsset created video into my app and then play it. But the video doesn't show in UICollectionView. How is it possible?

I write this code in View Did Load.

_collectionView.dataSource=self;
_collectionView.delegate=self;
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];

_allVideos = [[NSMutableArray alloc] init];
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];

[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
 {
     if (group)
     {
         [group setAssetsFilter:[ALAssetsFilter allVideos]];
         [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
          {
              if (asset)
              {
                  _dic = [[NSMutableDictionary alloc] init];
                  ALAssetRepresentation *defaultRepresentation = [asset defaultRepresentation];
                  NSString *uti = [defaultRepresentation UTI];
                  NSURL  *videoURL = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:uti];
                  NSString *title = [NSString stringWithFormat:@"video %d", arc4random()%100];
                  UIImage *image = [self imageFromVideoURL:videoURL];
                  [_dic setValue:image forKey:@"image"];
                  [_dic setValue:title forKey:@"name"];
                  [_dic setValue:videoURL forKey:@"url"];
                  //[_allVideos addObject:_dic];
                  [_allVideos addObject:asset];
                  [_collectionView reloadData];
              }
          }];
     }
 }
                          failureBlock:^(NSError *error)
{
    NSLog(@"error enumerating AssetLibrary groups %@\n", error);
}];
}

And One method is

 - (UIImage *)imageFromVideoURL:(NSURL*)videoURL
{
// result
UIImage *image = nil;

// AVAssetImageGenerator
AVAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];;
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform = YES;

// calc midpoint time of video
Float64 durationSeconds = CMTimeGetSeconds([asset duration]);
CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0, 600);

// get the image from
NSError *error = nil;
CMTime actualTime;
CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:midpoint actualTime:&actualTime error:&error];

if (halfWayImage != NULL)
{
    // CGImage to UIImage
    image = [[UIImage alloc] initWithCGImage:halfWayImage];
    [_dic setValue:image forKey:@"name"];
    NSLog(@"Values of dictionary==>%@", _dic);
    NSLog(@"Videos Are:%@",videoURL);
    CGImageRelease(halfWayImage);
}
return image;
}

And I wrote this in UICollectionView:

 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return _allVideos.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

NSLog(@"allvideo %@", _allVideos);
ALAsset *alasset = [_allVideos objectAtIndex:indexPath.row];
UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
imageview.image = [UIImage imageWithCGImage:alasset.thumbnail];
[cell.contentView addSubview:imageview];

return cell;

}

Upvotes: 1

Views: 971

Answers (1)

Divyesh Dobariya
Divyesh Dobariya

Reputation: 125

Write the code in ViewDidLoad

assets = [[NSMutableArray alloc] init];
_library = [[ALAssetsLibrary alloc] init];

UIImage *viewImage;

[_library writeImageToSavedPhotosAlbum:[viewImage CGImage] orientation:(ALAssetOrientation)[viewImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
    if (error) {
        NSLog(@"error");
    } else {
        NSLog(@"url %@", assetURL);


    }
}];
[_library enumerateGroupsWithTypes:ALAssetsGroupAll  usingBlock:^(ALAssetsGroup *group, BOOL *stop){

    if (group != NULL) {

        [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop){


            if ([[result valueForProperty:ALAssetsGroupPropertyName] isEqual:@"VideoMaker"])  {
                NSLog(@"asset: %@", result);
                [assets addObject:result];
            }

        }];
    }

    [self.collectionView reloadData];
    //[self.activity stopAnimating];
    //[self.activity setHidden:YES];

}
                     failureBlock:^(NSError *error){

                         NSLog(@"failure"); }];`} `

In this assets is NSMutableArray and library is the ALAssetLibrary.

In UIcollectionviewcell cell for rowAtIndexpath method

-(VideoCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    VideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
    ALAsset *asset = [assets objectAtIndex:indexPath.row];
    [cell.videoImageView setImage:[UIImage imageWithCGImage:[asset thumbnail]]];
    return cell;
}

Upvotes: 1

Related Questions