Kishore Suthar
Kishore Suthar

Reputation: 2983

Get all Images from iphone image library and show in UICollectionView

I want to show all the images from iphone image library to UICollectionView but after finished getAllPictures method, allPhotosCollected: method is not calling and images is not showing into 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];
}

Upvotes: 0

Views: 3196

Answers (2)

Arpit Dongre
Arpit Dongre

Reputation: 1713

You forgot to reload collectionView data, insert this line of code after fetching all images:

[self.collectionView reloadData];

However, I would recommend you to use Photos Framework instead of ALAssetsLibrary as ALAssetsLibrary is deprecated. Here (link) is an example of Photos Framework getting all videos, you have to modify it for getting Photos instead of Videos.

Upvotes: 0

Mannam Brahmaiah
Mannam Brahmaiah

Reputation: 2283

Can you please see the following code. I hope it will be helpful to you.

Get all image from gallery

View Controller header(.h) file..

#import <UIKit/UIKit.h>
#import <AssetsLibrary/AssetsLibrary.h> 

@interface getPhotoLibViewController : UIViewController
{
    ALAssetsLibrary *library;
    NSArray *imageArray;
    NSMutableArray *mutableArray;
}

-(void)allPhotosCollected:(NSArray*)imgArray;

@end

implementation file(.m)

declare global count variable as

static int count=0;

@implementation getPhotoLibViewController

-(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]]];

                 if ([mutableArray count]==count)
                 {
                     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
 {
      //write your code here after getting all the photos from library...
      NSLog(@"all pictures are %@",imgArray);
 }

@end Use getAllPicture method to get photos from photo library.

OR You can have a look at this blog http://mutiselectimagepicker.blogspot.in/2014/08/imageselect-to-allow-multiple-selection.html

Upvotes: 2

Related Questions