如何将video从ALAsset显示到UICollectionview ios

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

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

 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显示video的所有缩略图:

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

并在这种方法: –

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

首先从ALAssetsGroupEnumerationResultsBlock(传递给-enumerateAssetsUsingBlock:的块中取出代码[_collectionView reloadData] ,因为它将为找到的所有AVAsset调用-reloadData。

Apple提供了一个示例应用程序“ MyImagePicker ”,可以完成您想要做的事情。 你可以从那里学习工作。