How to access Images from Saved Images programaically in iphone WITHOUT UImagePickerController?


拜拜、爱过 提交于 2019-12-05 00:15:26

问题


I know how to let user select an image from UIImagePickerController, but I don't want that. I just want to have NSArray of images stored in the phone, but I don't want to involve user (to select a one and then have that image...),rather, I have created my own custom Image selector controller and want to have source as the gallary.


回答1:


You can easily do that using the AVFoundation and AssetsLibrary framework. 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");                                 
         }];
    }
}


来源:https://stackoverflow.com/questions/9736864/how-to-access-images-from-saved-images-programaically-in-iphone-without-uimagepi

标签

工具导航Map