To Get all Images from Photo Library in iphone

Agrawal Piyush picture Agrawal Piyush · Jul 7, 2012 · Viewed 12.7k times · Source

I trying to retrive images from Photo Library & display in my app using AssetsLibrary. I got the URL path of photos but I don't know how to get photo through it.

My Code is as Follow:

   NSMutableArray* assetURLDictionaries = [[NSMutableArray 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]];

                NSLog(@"result is:%@",result);
                NSLog(@"asset URLDictionary is:%@",assetURLDictionaries);
                NSURL *url= (NSURL*) [[result defaultRepresentation]url]; 

                [library assetForURL:url
                         resultBlock:^(ALAsset *asset) { [assetsp addObject:asset]; }
                        failureBlock:^(NSError *error){ NSLog(@"test:Fail"); } ]; 
           } 
       }
   };


   NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
   void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop){
        NSLog(@"hi");
        if(group != nil) {
            [group enumerateAssetsUsingBlock:assetEnumerator];
            [assetGroups addObject:group];

            NSLog(@"Number of assets in group :%d",[group numberOfAssets]);           
        }
   };

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

   [library enumerateGroupsWithTypes:ALAssetsGroupAll
                          usingBlock:assetGroupEnumerator
                        failureBlock:^(NSError *error) {NSLog(@"A problem occurred");}];

Answer

The iOSDev picture The iOSDev · Jul 7, 2012

This code is fetched from some ELCImagePickerController example from here Some modifications are done for simplification.

May this will help you

[self.assetGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) 
     {         
         if(result == nil) 
             return;
         UIImageView *assetImageView = [[UIImageView alloc] initWithFrame:viewFrames];
        [assetImageView setContentMode:UIViewContentModeScaleToFill];
        [assetImageView setImage:[UIImage imageWithCGImage:[result thumbnail]]];
     }];    

Happy Coding :)

NEW ANSWER

Just use

[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]

to store image directly instead of storing the asset at the time of storing asset in assetsp

just as follow

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

                NSLog(@"result is:%@",result);
                NSLog(@"asset URLDictionary is:%@",assetURLDictionaries);
                NSURL *url= (NSURL*) [[result defaultRepresentation]url]; 

                [library assetForURL:url
                         resultBlock:^(ALAsset *asset) { //Your line
                                                           //[assetsp addObject:asset];
                                                         //My Changed line will store image directly to assetsp
                                                           [assetsp addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];
                                                       }
                        failureBlock:^(NSError *error){ NSLog(@"test:Fail"); } ]; 
           } 
       }
   };

NEW ANSWER 1

Try this code to store the ALAsset data in mutable array

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

    for(ALAsset *asset in _assets) {

        NSMutableDictionary *workingDictionary = [[NSMutableDictionary alloc] init];
        [workingDictionary setObject:[asset valueForProperty:ALAssetPropertyType] forKey:@"UIImagePickerControllerMediaType"];
        [workingDictionary setObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]] forKey:@"UIImagePickerControllerOriginalImage"];
        [workingDictionary setObject:[[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]] forKey:@"UIImagePickerControllerReferenceURL"];

        [returnArray addObject:workingDictionary];

and to get the image in UIImageView just do this

NSDictionary *dict = [info objectAtIndex:i];
        UIImageView *imageview = [[UIImageView alloc] initWithImage:[dict objectForKey:UIImagePickerControllerOriginalImage]];
        [imageview setContentMode:UIViewContentModeScaleAspectFit];