使用ALAsset显示照片

时间:2014-02-22 17:32:32

标签: ios alassetslibrary alasset

ALAssetsLibraryGroupsEnumerationResultsBlock groupsEnumerationBlock = ^(ALAssetsGroup *group, BOOL *stop) {
    if ([groupName compare:[group valueForProperty:ALAssetsGroupPropertyName]] == NSOrderedSame){
        self.assetsGroup = group;
        NSLog(@"Has found the target group");
        return ;
    }
    ALAssetsGroupEnumerationResultsBlock assetsEnumerationBlock = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
        if (result) {
            [self.assets addObject:result];
            NSLog(@"%@",self.assets);
        }
    };
    ALAssetsFilter *onlyPhotosFilter = [ALAssetsFilter allPhotos];
    [self.assetsGroup setAssetsFilter:onlyPhotosFilter];
    [self.assetsGroup enumerateAssetsUsingBlock:assetsEnumerationBlock];
};

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;
    }
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error"
                                                   message:[NSString stringWithFormat:@"%@",errorMessage]
                                                  delegate:self
                                         cancelButtonTitle:@"OK"
                                         otherButtonTitles:nil, nil];
    [alert show];
};

NSUInteger groupTypes = ALAssetsGroupAlbum | ALAssetsGroupEvent | ALAssetsGroupFaces | ALAssetsGroupSavedPhotos;
[self.library enumerateGroupsWithTypes:groupTypes
                            usingBlock:groupsEnumerationBlock failureBlock:failureBlock];

我想在collectinView中显示特定相册中的照片。我可以在enumerationBlock中获取数据,但不能使用self.assets来显示集合。我对块不是很熟悉。我怎么能解决问题?

1 个答案:

答案 0 :(得分:0)

ALAssets中显示UIImageView

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
  UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
  ALAsset *asset = [self.assets objectAtIndex:indexPath.row];
    UIImage *image = [[asset defaultRepresentation] fullScreenImage];

  [cell.imageView setImage:image]

  return cell;
}

如果您在找到所需资产后致电[self.collectionView reloadData],那么您应该好好去。