如何将视频从ALAsset显示到UICollectionview ios [英] How to display video from ALAsset to UICollectionview ios

查看:121
本文介绍了如何将视频从ALAsset显示到UICollectionview ios的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的代码中尝试使用 ALAsset 从照片库中获取所有视频。现在,我想将所有视频显示到 UICollectionview 但它似乎没有显示任何内容。请给我一些建议。提前致谢。

I tried below code to get all videos from photo library by using ALAsset. For now, i want to display all videos to a UICollectionview but it doesn't seem to display anything. Please give me some advice. Thanks in advance.

-ViewDidLoad():从照片库中获取所有视频

-ViewDidLoad() : get all videos from Photo Library

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];
                  [_collectionView reloadData];
              }
          }];
     }
 }
 failureBlock:^(NSError *error)
{
    NSLog(@"error enumerating AssetLibrary groups %@\n", error);
}];

-imageFromVideoURL():

-imageFromVideoURL():

    - (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;
}

开始向UICollectionView显示所有视频缩略图:

Start to display all thumbnails of video to 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];

        return cell;
    }


推荐答案

替换此行: -

              [allVideos addObject:dic];

使用

              [allVideos addObject: asset];

在这种方法中: -

And in this method:-

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

    NSLog(@"allvideo %@", allVideos);
    ALAsset *alasset = [allVideos objectAtIndex:indexPath.row];
    yourImageView.image = [UIImage imageWithCGImage:alasset.thumbnail];

 }

这篇关于如何将视频从ALAsset显示到UICollectionview ios的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆