如何使用 ALAsset 从图库访问智能文件夹

Posted

技术标签:

【中文标题】如何使用 ALAsset 从图库访问智能文件夹【英文标题】:How to access smart folder from gallery using ALAsset 【发布时间】:2017-11-30 07:39:44 【问题描述】:

我正在制作一个应用程序,我想从图库中获取所有专辑名称的列表,包括智能文件夹(收藏夹、屏幕截图。这是一个旧应用程序,我们使用 ALAsset 来访问我们应用程序中的图库。

有没有什么方法可以让我们使用 ALAssetLibrary 访问智能文件夹?

【问题讨论】:

【参考方案1】:

此代码有帮助。

   #import <AssetsLibrary/AssetsLibrary.h>

    @property (nonatomic, strong) ALAssetsLibrary *_assetsLibrary;

    - (ALAssetsLibrary *)defaultAssetsLibrary 
        static dispatch_once_t pred = 0;
        static ALAssetsLibrary *library = nil;
        dispatch_once(&pred, ^
            library = [[ALAssetsLibrary alloc] init];
        );
        return library;
    

    -(void) getgalleryPic
    
        if (self.photos == nil) 
            self.photos = [[NSMutableArray alloc] init];
        else
        
            [self.photos removeAllObjects];
        

        PHAuthorizationStatus status = [phphotoLibrary authorizationStatus];

        if (status == PHAuthorizationStatusAuthorized) 
            // Access has been granted.
            NSMutableArray *collector = [[NSMutableArray alloc] initWithCapacity:0];
            ALAssetsLibrary *library = [self defaultAssetsLibrary];

            [library enumerateGroupsWithTypes:ALAssetsGroupAll
                                   usingBlock:^(ALAssetsGroup *group, BOOL *stop)
             
                 [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
                  
                      if (asset) 
                          [collector addObject:asset];
                      else
                      
                          self.photos = [[[collector reverseObjectEnumerator] allObjects] mutableCopy];
                          NSLog(@"photo lenght %lu",(unsigned long)[self.photos count]);
                          [_collectionVW reloadData];
                      
                  ];
             
                                 failureBlock:^(NSError *error)  NSLog(@"Boom!!!");
             ];
        

        else if (status == PHAuthorizationStatusDenied) 
            // Access has been denied.
        

        else if (status == PHAuthorizationStatusNotDetermined) 

            // Access has not been determined.
            [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) 

                if (status == PHAuthorizationStatusAuthorized) 
                    // Access has been granted.
                    NSMutableArray *collector = [[NSMutableArray alloc] initWithCapacity:0];
                    ALAssetsLibrary *library = [self defaultAssetsLibrary];

                    [library enumerateGroupsWithTypes:ALAssetsGroupAll
                                           usingBlock:^(ALAssetsGroup *group, BOOL *stop)
                     
                         [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
                          
                              if (asset) 
                                  [collector addObject:asset];
                              else
                              
                                  self.photos = [[[collector reverseObjectEnumerator] allObjects] mutableCopy];
                                  NSLog(@"photo lenght %lu",(unsigned long)[self.photos count]);
                                  [_collectionVW reloadData];
                              
                          ];
                     
                                         failureBlock:^(NSError *error)  NSLog(@"Boom!!!");
                     ];
                

                else 
                    // Access has been denied.
                
            ];
            else if (status == PHAuthorizationStatusRestricted) 
            // Restricted access - normally won't happen.
        


    

【讨论】:

ios 11.3 和 11.4 系统有些不工作,如何解决这个

以上是关于如何使用 ALAsset 从图库访问智能文件夹的主要内容,如果未能解决你的问题,请参考以下文章

如何获取共享照片流 ALAsset 的 ALAssetRepresentation?

如何将视频从 ALAsset 显示到 UICollectionview ios

如何将视频从 ALAsset 显示到 UICollectionview Cell

如何使用 ALAsset 获得图像的裁剪版本?

如何从 ALAsset 获取 exif 信息?

如何从 ALASSET ios 获取视频大小