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

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

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

【问题讨论】:

    标签: objective-c iphone alassetslibrary alasset


    【解决方案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 系统有些不工作,如何解决这个
    猜你喜欢
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多