2014-01-10 7 views
6

ALAssetを使用して、フォトライブラリからすべてのビデオを取得するには、以下のコードを試しました。今のところ、すべてのビデオをUICollectionviewに表示したいのですが、何も表示していないようです。助けてください。前もって感謝します。ALAssetからUICollectionview iosにビデオを表示する方法

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

答えて

1

このライン置き換え

- (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]をALAssetsGroupEnumerationResから取り出しますultsBlock(あなたが-enumerateAssetsUsingBlock:に渡されたブロック)は、見つかったすべてのAVAssetsに対して-reloadDataを呼び出します。

あなたがしたいことを正確に行うリンゴのサンプルアプリケーション "MyImagePicker"があります。そこから作業を勉強することができます。

   [allVideos addObject:dic]; 

   [allVideos addObject: asset]; 

ではまた、この方法では: - - :

+0

おかげで、しかし、もし外のコード[_collectionView reloadData] AKE ALAssetsGroupEnumerationResultsBlock、uicollectionviewはビデオのサムネイルを表示しません。 – user3168540

+0

私のアプリでは、列挙ブロックの後に '-reloadData'と書いてあり、ブロックの後に同期して実行されることがわかりました。また、提供されたリンク上でアプリケーションをチェックすると、リンゴは '-viewDillAppear'の' -viewWillAppear'と '-reloadData'に列挙ブロックを呼び出しました –

+0

ありがとう。私はそれを成功させましたが、今私はビデオのサイズを取得するには固執しています。ビデオのサイズを取得する方法を教えてください。 – user3168540

関連する問題