1

我試過這段代碼將所有ALAsset創建的視頻添加到我的應用程序中,然後播放它,但視頻不顯示在UICollectionView中,所以它是如何可能的。 感謝高級。如何顯示從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"); }];`} ` 

在此資產是NSMutableArray和是ALAssetLibrary。

在用於rowAtIndexpath方法UIcollectionviewcell細胞

-(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; 
} 
相關問題