2013-08-29 4 views
7

次のコードから写真ライブラリからビデオを入手しようとしています。しかし、画像リストも取得しています。私は間違って何をしていますか?ALAssetsLibraryを使ってビデオを取得するにはどうすればいいですか

NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init]; 
xy =[[NSMutableArray alloc]init]; 
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 

void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) { 
    if(result != nil) { 
     if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) { 
      [assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]]; 

      NSLog(@"result is:%@",result); 
      NSLog(@"asset URLDictionary is:%@",assetURLDictionaries); 
      NSURL *url= (NSURL*) [[result defaultRepresentation]url]; 

      [library assetForURL:url 
        resultBlock:^(ALAsset *asset) { 
         [xy addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]]; 
         NSLog(@" xy is:%@",xy); 
         image =[ [UIImageView alloc ] initWithImage:[xy objectAtIndex:0]]; 
         NSLog(@"image is:%@",image); 
        } 
        failureBlock:^(NSError *error){ NSLog(@"test:Fail"); } ]; 
     } 
    } 
}; 

NSMutableArray *assetGroups = [[NSMutableArray alloc] init]; 
void (^ assetGroupEnumerator) (ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) { 
    NSLog(@"hello"); 
    if(group != nil) { 
     [group enumerateAssetsUsingBlock:assetEnumerator]; 
     [assetGroups addObject:group]; 
     NSLog(@"Number of assets in group :%d",[group numberOfAssets]); 
     NSLog(@"asset group is:%@",assetGroups); 
    } 
}; 

assetGroups = [[NSMutableArray alloc] init]; 

[library enumerateGroupsWithTypes:ALAssetsGroupAll 
         usingBlock:assetGroupEnumerator 
        failureBlock:^(NSError *error) {NSLog(@"A problem occurred");}]; 

答えて

17

列挙中にALAssetsFilterをグループに追加する必要があります。ここで基本的な例は次のとおり

ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init]; 

[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) { 
    if (group) { 
     [group setAssetsFilter:[ALAssetsFilter allVideos]]; 
     [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){ 
      if (asset){ 

       NSDictionary *meta = [[asset defaultRepresentation] metadata]; 

      } 
     }]; 
    } 
} failureBlock:^(NSError *error) { 
    NSLog(@"error enumerating AssetLibrary groups %@\n", error); 
}]; 

は、将来の参照のために、available filtersは:[ドキュメント]に記載

- allPhotos 
- allVideos 
- allAssets 
+0

注(https://developer.apple.com/library/ios/documentation /AssetsLibrary/Reference/ALAssetsLibrary_Class/Reference/Reference.html#//apple_ref/doc/constant_group/Types_of_Asset) 'ALAssetsGroupAll'は、' ALAssetsGroupLibrary'を除いてすべてのグループタイプをORするのと同じです。 iTunesも必要な場合は、 'ALAssetsGroupAll | ALAssetsGroupLibrary'には「iTunesから同期されるすべてのアセットが含まれます」と表示されます。 – bcattle

関連する問題