To Get all Images from Photo Library in iphone

12,726

Solution 1

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

Solution 2

My code is as follows:

- (void)viewDidLoad
{
    carousel.type = iCarouselTypeCoverFlow2;
    [super viewDidLoad];
    xy =[[NSMutableArray alloc]init];

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

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

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

                [library assetForURL:url
                         resultBlock:^(ALAsset *asset) {
                             [xy addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];
                             NSLog(@" xy is:%@",xy);
                             image =[ [UIImageView alloc ] initWithImage:[xy objectAtIndex:0]];
                             NSLog(@"image is:%@",image);
                         }
                         failureBlock:^(NSError *error){ NSLog(@"test:Fail"); } ]; 
            } 
        }
    };

    NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
    void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
        NSLog(@"hello");
        if(group != nil) {
            [group enumerateAssetsUsingBlock:assetEnumerator];
            [assetGroups addObject:group];
            NSLog(@"Number of assets in group :%d",[group numberOfAssets]);
            NSLog(@"asset group is:%@",assetGroups);
        }
    };

    assetGroups = [[NSMutableArray alloc] init];

    [library enumerateGroupsWithTypes:ALAssetsGroupAll
             usingBlock:assetGroupEnumerator
             failureBlock:^(NSError *error) {NSLog(@"A problem occurred");}];
Share:
12,726
Agrawal Piyush
Author by

Agrawal Piyush

Updated on June 05, 2022

Comments

  • Agrawal Piyush
    Agrawal Piyush almost 2 years

    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");}];
    
  • Agrawal Piyush
    Agrawal Piyush almost 12 years
    I got all the images but it is not displayed
  • The iOSDev
    The iOSDev almost 12 years
    What does this mean got all images but not displayed?
  • Agrawal Piyush
    Agrawal Piyush almost 12 years
    yes i init it & i got the all images in NSLOG but they are not displayed on view
  • Agrawal Piyush
    Agrawal Piyush almost 12 years
    NSLOG ANSWER:"<UIImage: 0x876ded0>", "<UIImage: 0x8761a80>", "<UIImage: 0x8738290>", "<UIImage: 0x874abf0>", "<UIImage: 0x87721b0>", "<UIImage: 0x8772b80>", "<UIImage: 0x876fab0>", "<UIImage: 0x876efa0>", "<UIImage: 0x876a870>", "<UIImage: 0x8773440>", "<UIImage: 0x87712b0>", "<UIImage: 0x876ec60>", "<UIImage: 0x87752c0>", "<UIImage: 0x8767ac0>" ) 2012-07-07 19:17:07.963 Carousel[5901:207] image is:<UIImageView: 0x8772340; frame = (0 0; 48 48); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x87357e0>>