如何将视频从 ALAsset 显示到 UICollectionview Cell
Posted
技术标签:
【中文标题】如何将视频从 ALAsset 显示到 UICollectionview Cell【英文标题】:How to display video from ALAsset to UICollectionview Cell 【发布时间】:2014-09-02 12:06:31 【问题描述】:我尝试使用此代码将所有 ALAsset 创建的视频添加到我的应用程序中,然后播放它,但视频没有显示在 UICollectionView 中,所以这是可能的。 感谢高级。
我在 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);
];
一种方法是
- (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];
UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
imageview.image = [UIImage imageWithCGImage:alasset.thumbnail];
[cell.contentView addSubview:imageview];
return cell;
【问题讨论】:
【参考方案1】:在 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"); ];` `
在这个 assets 中是 NSMutableArray 而 library 是 ALAssetLibrary。
在 UIcollectionviewcell 单元格中的 rowAtIndexpath 方法
-(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;
【讨论】:
以上是关于如何将视频从 ALAsset 显示到 UICollectionview Cell的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 ALAsset URL 在 MPMoviePlayerController 上播放视频?