从 ALAssetsLibrary 获取所有照片

Posted

技术标签:

【中文标题】从 ALAssetsLibrary 获取所有照片【英文标题】:Getting all photos from ALAssetsLibrary 【发布时间】:2016-03-07 12:57:43 【问题描述】:

这只会返回照片中的一些相册,而我获取的相册不包含与照片应用中的图片相同数量的资产。

NSMutableArray *groups = [[NSMutableArray alloc] init];
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll
    usingBlock:^(ALAssetsGroup *group, BOOL *stop) 
      if (group) 
          [group setAssetsFilter:[ALAssetsFilter allPhotos]];
          [groups addObject:group];
       else 
          block([groups copy], nil);
      
    
    failureBlock:^(NSError *error) 
      block(nil, error);
    ];

我需要做什么才能获得全部 1000 个?

【问题讨论】:

您可以使用 Photos 和 AVFoundation 库尝试 PHAsset。我正在使用 PHAsset 并访问所有图像,但我在 swift2.0 中的代码 How can I get my alassets from my library in date order across all photo albums?的可能重复 【参考方案1】:

类似

- (void)loadLibraryGroups

    if (self.assetsLibrary == nil) 
        _assetsLibrary = [[ALAssetsLibrary alloc] init];
    
    if (self.groups == nil) 
        _groups = [[NSMutableArray alloc] init];
     else 
        [self.groups removeAllObjects];
    

    // setup our failure view controller in case enumerateGroupsWithTypes fails
    ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError *error) 

        NSString *errorMessage = nil;
        switch ([error code]) 
            case ALAssetsLibraryAccessUserDeniedError:
            case ALAssetsLibraryAccessGloballyDeniedError:
                errorMessage = @"The user has declined access to it.";
                break;
            default:
                errorMessage = @"Reason unknown.";
                break;
        
    ;

    // emumerate through our groups and only add groups that contain photos
    ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) 

        ALAssetsFilter *onlyPhotosFilter = [ALAssetsFilter allPhotos];
        [group setAssetsFilter:onlyPhotosFilter];
        if ([group numberOfAssets] > 0)
        
            NSLog(@"group ==> %@",group);
            [self.groups addObject:group];
        
        else
        
            NSLog(@"Empty Groups load all items");

            for (ALAssetsGroup *group in self.groups) 
                [self loadImageForEachGroup:group];
            

        
    ;

    // enumerate only photos
    NSUInteger groupTypes = ALAssetsGroupAlbum | ALAssetsGroupEvent | ALAssetsGroupFaces | ALAssetsGroupSavedPhotos;
    [self.assetsLibrary enumerateGroupsWithTypes:groupTypes usingBlock:listGroupBlock failureBlock:failureBlock];

    NSLog(@"finish load groups");


- (void)loadImageForEachGroup:(ALAssetsGroup *)group

    if (!self.assets) 
        _assets = [[NSMutableArray alloc] init];
     else 
        [self.assets removeAllObjects];
    

    ALAssetsGroupEnumerationResultsBlock assetsEnumerationBlock = ^(ALAsset *result, NSUInteger index, BOOL *stop) 
        if (result != nil) 
            [self.assets addObject:result];
         else 
            [self.collectionView reloadData];
        
    ;

    ALAssetsFilter *onlyPhotosFilter = [ALAssetsFilter allPhotos];
    [group setAssetsFilter:onlyPhotosFilter];
    [group enumerateAssetsUsingBlock:assetsEnumerationBlock];

并加载到集合中

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

    return self.assets.count;


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

    static NSString *CellIdentifier = @"photoCell";

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    // Configure the cell

    // load the asset for this cell
    ALAsset *asset = self.assets[indexPath.row];
    CGImageRef thumbnailImageRef = [asset thumbnail];
    UIImage *thumbnail = [UIImage imageWithCGImage:thumbnailImageRef];

    // apply the image to the cell
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:1];
    imageView.image = thumbnail;

    return cell;

【讨论】:

但这只是问题..[group numberOfAssets] 不会返回与 Photo 包含的相同数量的资产.....

以上是关于从 ALAssetsLibrary 获取所有照片的主要内容,如果未能解决你的问题,请参考以下文章

我如何仅使用 ALAssetsLibrary 获取视频

获取 iOS Photo Gallery 中包含人脸的所有照片的数组

访问 iPhone 照片库中的所有照片?

如何根据目标c中的日期获取照片

尝试使用 ALAssetsLibrary 检索一组照片

加快从 ALAssetsLibrary 读取和填充 UITableView 的可能方法