如何在iOS中将ALAsset的视频显示到UICollectionview?

6
我尝试使用以下代码使用ALAsset从照片库中获取所有视频。现在,我想将所有视频显示到UICollectionview上,但似乎没有显示任何内容。请给我一些建议。提前致谢。
- ViewDidLoad():从照片库获取所有视频
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():

    - (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中显示所有视频缩略图:

 - (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;
    }
2个回答

1
替换此行:-
              [allVideos addObject:dic];

带有

              [allVideos addObject: asset];

在这个方法中:-
- (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];

 }

0
首先,将代码[_collectionView reloadData]从ALAssetsGroupEnumerationResultsBlock(您传递给-enumerateAssetsUsingBlock:的块)中取出,因为它会调用找到的所有AVAssets的-reloadData
苹果有一个名为“MyImagePicker”的示例应用程序,可以完成您想要做的确切事情。您可以从中学习工作原理。

谢谢,但是如果将代码[_collectionView reloadData]移出ALAssetsGroupEnumerationResultsBlock,UICollectionView就不会显示视频缩略图。 - user3168540
在我的应用程序中,我在枚举块后编写了“-reloadData”,我发现它会在块之后同步执行。此外,如果您检查提供的链接上的应用程序,苹果已在“-viewWillAppear”和“-viewDidAppear”中调用了枚举块和“-reloadData”。 - Gaurav Singh
谢谢。我已经成功了,但现在我卡在获取视频大小上了。你能告诉我如何获取视频大小吗? - user3168540
你可以从 ALAsset 中获取一个 ALAssetRepresentation,然后你可以找到不同的选项来获取大小、尺寸等信息。 - Gaurav Singh
是的,谢谢。还有一件事:您知道如何在取消选择uicollectionview中的叠加图像吗?我在选择时添加了叠加图像,现在我卡在了当滚动时,我的单元格自动刷新。我不想它自动刷新。 - user3168540

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接