如何在没有UIimagePickerController的iphone中以编程方式访问已保存图像中的图像?

时间:2012-03-16 12:02:45

标签: iphone ios xcode

我知道如何让用户从UIImagePickerController中选择一个图像,但我不希望这样。 我只想在手机中存储NSArray图像,但我不想让用户参与(选择一个然后拥有该图像......),而是我创建了自己的自定义图像选择器控制器并且想要有源作为gallary。

1 个答案:

答案 0 :(得分:9)

您可以使用AVFoundation和AssetsLibrary框架轻松完成此操作。以下是访问所有照片的代码:

-(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");                                 
         }];
    }
}