ALAssetsLibrary get all videos

天大地大妈咪最大 提交于 2019-12-11 19:07:12

问题


I am making a camera app where I want to get to all of the videos users have created on their iPhone.

Currently the code I have gets the videos from user Camera Roll only. Some my users have complained that they have multiple custom folders created under their Photo Album app and they store some videos in there. Since my code only looks at Camera Roll, it doesn't pickup the movies from their other folders. Is it possible that I can get to their other folders?

This is what I have so far.

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop)
     {
         [group enumerateAssetsUsingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop)
         {
              if (alAsset)
              {
                  ALAssetRepresentation *representation =[alAsset defaultRepresentation];
                  NSURL *url = [representation url];
                  NSString *assetType=[alAsset valueForProperty:ALAssetPropertyType];


                  //videos only
                  if ([assetType isEqualToString:@"ALAssetTypeVideo"])
                  {
                   .....

回答1:


You got to create a filter for the assets, something like this:

ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) {  

    ALAssetsFilter *allVideosFilter = [ALAssetsFilter allVideos];
    [group setAssetsFilter:allVideosFilter];
    //...
};

Options for filters are: - allAssets - allVideos - allPhotos

Hope this helps




回答2:


To get media that was synced from iTunes you need to use ALAssetsGroupLibrary. Here you can find all possible variants for ALAssetsGroupType. So just change

[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:...

to

[library enumerateGroupsWithTypes:(ALAssetsGroupSavedPhotos | ALAssetsGroupLibrary) usingBlock:...



回答3:


This retrieves all videos, including any the user has synced from iTunes:

// Enumerate just the photos and videos by using ALAssetsGroupSavedPhotos

[library enumerateGroupsWithTypes:ALAssetsGroupAll | ALAssetsGroupLibrary
                       usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
    if (group != nil)
    {
        // Within the group enumeration block, filter to enumerate just videos.
        [group setAssetsFilter:[ALAssetsFilter allVideos]];
        [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
            if (result) {
                // Do whatever you need to with `result`
            }
        }];
    } else {
        // If group is nil, we're done enumerating
        // e.g. if you're using a UICollectionView reload it here
        [collectionView reloadData];
    }
} failureBlock:^(NSError *error) {
    // If the user denied the authorization request
    NSLog(@"Authorization declined");
}];

Note the ALAssetsGroupAll | ALAssetsGroupLibrary.

According to the docs ALAssetsGroupAll is "the same as ORing together all the group types except for ALAssetsGroupLibrary". So we also add ALAssetsGroupLibrary which "includes all assets that are synced from iTunes".



来源:https://stackoverflow.com/questions/20613241/alassetslibrary-get-all-videos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!