How to access images from saved images programmatically in iphone WITHOUT UImagePickerController?

I know how to allow the user to select an image from the UIImagePickerController, but I do not want this. I just want to have an NSArray of images stored on the phone, but I don't want to attract the user (to select one and then have this image ...), rather, I created my own custom image selection controller and want to have the source as a gallery.

+5
source share
1 answer

You can easily do this using the AVFoundation and AssetsLibrary structure. Here is the code to access all the photos:

-(void)addPhoto:(ALAssetRepresentation *)asset
{
    //NSLog(@"Adding photo!");
    [photos addObject:asset];
}

-(void)loadPhotos
{
    photos = [[NSMutableArray alloc] init];    
    library = [[ALAssetsLibrary alloc] init];    

    // Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
    if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
    {
        [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) 
        {        
             // Within the group enumeration block, filter if necessary
             [group setAssetsFilter:[ALAssetsFilter allPhotos]];           
             [group enumerateAssetsUsingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop)
              {                                 
                  // The end of the enumeration is signaled by asset == nil.            
                  if (alAsset)
                  {
                      ALAssetRepresentation *representation = [alAsset defaultRepresentation];                      
                      [self addPhoto:representation];                      
                  }       
                  else
                  {
                      NSLog(@"Done! Count = %d", photos.count);
                      //Do something awesome
                  }
              }];
         }
         failureBlock: ^(NSError *error) {
         // Typically you should handle an error more gracefully than this.
         NSLog(@"No groups");                                 
         }];
    }
}
+9
source

All Articles