Search code examples
ipadphotos

Ipad Application how to get images from custom album


I am developing an iPad application in which I have saved images to custom album using This.

Now I want to get all the images from that custom folder and I need to show all those in a animated UIImageView.

I know how to set animation but I want to know how to get all the images from particular custom folder.


Solution

  • See this code I used to load the images from the custom album. I have used the same sample code to store my images in custom album.

    NSMutableArray *tempArray = [[NSMutableArray alloc] init];
        self.assetGroups = tempArray;
    
        library = [[ALAssetsLibrary alloc] init];      
    
        // Load Albums into assetGroups
    
            // Group enumerator Block
            void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) 
            {
                if (group == nil) 
                {
                    return;
                }
                if([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:kAlbumName])
                {
                    [self.assetGroups addObject:group];
                    [self reloadTableView];
                    return;
                }
            };
    
            // Group Enumerator Failure Block
            void (^assetGroupEnumberatorFailure)(NSError *) = ^(NSError *error) {
    
                CustomAlertView * alert = [[CustomAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"Album Error: %@ - %@", [error localizedDescription], [error localizedRecoverySuggestion]] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                [alert show];
    
                NSLog(@"A problem occured %@", [error description]);                                     
            };  
    
            // Enumerate Albums
            [library enumerateGroupsWithTypes:ALAssetsGroupAll
                                   usingBlock:assetGroupEnumerator 
                                 failureBlock:assetGroupEnumberatorFailure];
    

    Here the kAlbumName is one string ivar which contains the custom album name.

    EDIT:1

    Above code just gives you the whole album selected with all it photos now to get those photos from album use the following code

    [self.assetGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) 
         {         
             if(result == nil) 
                 return;
         CGRect viewFrames = kThumbSize;//CGRectMake(0, 0, 75, 75);
             UIImageView *assetImageView = [[UIImageView alloc] initWithFrame:viewFrames];
            [assetImageView setContentMode:UIViewContentModeScaleToFill];
            [assetImageView setImage:[UIImage imageWithCGImage:(__bridge CGImageRef)([result originalAsset])]];
         }];
    

    NOTE: Instead of kThumbSize define your CGRectMake() as commented.

    Enjoy coding :) Happy Day :)