Как отображать видео с ALAsset на UICollectionview Cell

Я пробовал этот код, чтобы добавить все созданные 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, ********** 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, ********** 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"); }];'} '

В этом активе NSMutableArray и библиотека - это 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;
}

licensed under cc by-sa 3.0 with attribution.