0

iPhoneの画像ライブラリからすべての画像を表示したいのですUICollectionViewが、getAllPicturesメソッドが終了した後、allPhotosCollectedメソッドが呼び出されず、画像が表示されませんUICollectionView

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self getAllPictures];
} 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [imageArray count];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"Cell";

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];

    recipeImageView.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]];
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]];

    return cell;
}


-(void)getAllPictures
{
    imageArray=[[NSArray alloc] init];
    mutableArray =[[NSMutableArray alloc]init];
    NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];

    library = [[ALAssetsLibrary alloc] init];

    void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
        if(result != nil) {
            if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
                [assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];

                NSURL *url= (NSURL*) [[result defaultRepresentation]url];

                [library assetForURL:url
                         resultBlock:^(ALAsset *asset) {
                             [mutableArray addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];



                                 imageArray=[[NSArray alloc] initWithArray:mutableArray];
                                 [self allPhotosCollected:imageArray];

                         }
                        failureBlock:^(NSError *error){ NSLog(@"operation was not successfull!"); } ];

            }
        }
    };

    NSMutableArray *assetGroups = [[NSMutableArray alloc] init];

    void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
        if(group != nil) {
            [group enumerateAssetsUsingBlock:assetEnumerator];
            [assetGroups addObject:group];
            count=[group numberOfAssets];
        }
    };

    assetGroups = [[NSMutableArray alloc] init];

    [library enumerateGroupsWithTypes:ALAssetsGroupAll
                           usingBlock:assetGroupEnumerator
                         failureBlock:^(NSError *error) {NSLog(@"There is an error");}];

}


-(void)allPhotosCollected:(NSArray*)imgArray
{
    NSLog(@"all pictures are %@",imgArray);
    [self.collectionView setDelegate:self];
    [self.collectionView setDataSource:self];
}
4

2 に答える 2

0

collectionView dataをリロードするのを忘れていました。すべての画像を取得した後に次のコード行を挿入してください。

[self.collectionView reloadData];

ただし、ALAssetsLibrary は非推奨であるため、ALAssetsLibrary の代わりに Photos Framework を使用することをお勧めします。ここ(リンク)は、すべてのビデオを取得する Photos フレームワークの例です。ビデオの代わりに写真を取得するように変更する必要があります。

于 2016-08-16T13:02:04.713 に答える